Skip to content

Instantly share code, notes, and snippets.

@john-clark
Last active September 15, 2022 15:44
Show Gist options
  • Save john-clark/5356220 to your computer and use it in GitHub Desktop.
Save john-clark/5356220 to your computer and use it in GitHub Desktop.
some windows admin stuff
Some windows commands I find useful.
appcmd set config /section:staticContent /+"[fileExtension=' * ',mimeType=' application/octet-stream ']"
@pause
@echo off
cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell "wevtutil.exe el | foreach-object {write-host "clearing $_"; wevtutil.exe cl "$_"}"
pause
rem = use this file by typing diskpart /s diskpart.ini
rem = CreatePartitions-UEFI.txt ==
rem = These commands are used with DiskPart to
rem = create partitions for a UEFI/GPT-based PC.
select disk 0
clean
convert gpt
rem = System partition =========================
create partition efi size=100
rem = NOTE: For Advanced Format 4Kn drives,
rem = change this value to size = 260
format quick fs=fat32 label="System"
assign letter="S"
rem = Microsoft Reserved (MSR) partition =======
create partition msr size=128
rem = Windows partition ========================
create partition primary
format quick fs=ntfs label="Windows"
assign letter="W"
exit
@ECHO OFF
setlocal enabledelayedexpansion
REM
REM *** dosomethingonalldirs.CMD ***
REM
IF "%1"=="" GOTO SYNTAX
IF "%2"=="" GOTO SYNTAX
SET _cmdvar=%*
SET _remove=%1
CALL SET _cmdline=%%_cmdvar:!_remove! =%%
IF EXIST %1/nul (
FOR /d %%i IN ("%1/*") DO (call :runcmd %%i)
GOTO :eof
) ELSE (
ECHO The folder %1 does not exist, try again.
)
GOTO END
:runcmd
CALL %_cmdline%%1
GOTO :eof
:SYNTAX
ECHO.
ECHO Syntax:
ECHO dosomethingonalldirs {subdir} {cmd}
ECHO.
ECHO Example: dosomethingonalldirs subdir echo
ECHO Will output all subdir names
ECHO.
:END
'mkshortcut /target:TargetName /shortcut:ShortcutName
set WshShell = WScript.CreateObject(“WScript.Shell” )
sStartmenuPath = WshShell.SpecialFolders(“StartMenu”)
set oShellLink = WshShell.CreateShortcut(sStartmenuPath & “” & Wscript.Arguments.Named(“shortcut”) & “.lnk”)
oShellLink.TargetPath = Wscript.Arguments.Named(“target”)
oShellLink.WindowStyle = 1
oShellLink.Save
@ECHO OFF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ::
:: FILENAME: SERVICE.CMD ::
:: PURPOSE: Trying to make windows service control like linux ::
:: USAGE: SERVICE.CMD /? or SERVICE.CMD --HELP or just type SERVICE.CMD ::
:: ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
call :Housekeeping
:: Command line error check:
IF "%1"=="" GOTO :Syntax
:: will do this better , just making basics work for now
IF "%1"=="/?" GOTO :About
IF "%1"=="--help" GOTO :About
IF "%1"=="--status-all" GOTO :CheckServiceStatusAll
IF "%1"=="status" GOTO :CheckServiceStatusAll
SET ServiceName=%1
SET ServiceCommand=%2
:: Process Commandline
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Check to see if there is a service match
FOR /F "tokens=2*" %%n IN ('SC query state^= all ^| findstr SERVICE_NAME') DO (
if /I "%%n" == "%ServiceName%" GOTO :CheckCommand
)
ECHO %ServiceName%: unrecognized service
GOTO :END
::check to see if %2 matches legal option if not
:CheckCommand
2>NULL CALL :CASE_%ServiceCommand%
IF ERRORLEVEL 1 (
ECHO Usage: service %ServiceName% {start^|stop^|restart^|status}
GOTO :END
)
Goto :END
:CASE_start
SC QUERY %ServiceName% | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 1 (
sc start %ServiceName% >NUL
::call some check to see if it gets started
ECHO %ServiceName% started
) ELSE (
ECHO %ServiceName% already started
)
GOTO :END_CASE
:CASE_stop
SC QUERY %ServiceName% | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 1 (
ECHO %ServiceName% already stopped
) ELSE (
sc stop %ServiceName% >NUL
::call some check to see if it gets stopped
ECHO %ServiceName% stopped
)
GOTO :END_CASE
:CASE_restart
SC QUERY %ServiceName% | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 1 (
ECHO %ServiceName% not started
sc start %ServiceName% >NUL
::call some check to see if it gets started
ECHO %ServiceName% started
) ELSE (
sc stop %ServiceName% >NUL
::call some check to see if it gets stopped
ECHO %ServiceName% stopped
:: PROBLEM!!! DOES NOT WAIT LONG ENOUGH (CHECK SHOULD FIX THIS)
sc start %ServiceName% >NUL
::call some check to see if it gets started
ECHO %ServiceName% started
)
GOTO :END_CASE
:CASE_enable
echo this section not done
echo %ServiceName% %ServiceCommand%
GOTO :END_CASE
:CASE_disable
echo this section not done
echo %ServiceName% %ServiceCommand%
GOTO :END_CASE
:CASE_status
SC QUERY %ServiceName% | FIND "STATE" | FIND "RUNNING" >NUL
IF errorlevel 1 ( ECHO [ STOPPED ] %ServiceName% is not running... ) ELSE ( ECHO [ RUNNING ] %ServiceName% is running... )
GOTO :END_CASE
:CASE_info
FOR /F "tokens=2* delims=:" %%n IN ('SC qc %ServiceName% ^| findstr DISPLAY_NAME') DO Set FullServiceName=%%n
FOR /F "tokens=2* delims=:" %%n IN ('SC qdescription %ServiceName% ^| findstr DESCRIPTION') DO Set Description=%%n
Echo %FullServiceName:~1% - %Description:~2%
GOTO :END_CASE
:END_CASE
VER > NUL
GOTO :EOF
:: Simple Commandline option
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Check Status of all Services
:CheckServiceStatusAll
FOR /F "tokens=2*" %%n IN ('SC query state^= all ^| findstr SERVICE_NAME') DO (
SET TmpServiceName=%%n
call :processService
)
GOTO END
:processService
if "%TmpServiceName%" == "" goto :EOF
FOR /F "tokens=4*" %%r IN ('SC query %TmpServiceName% ^| findstr STATE') DO (
SET ServiceStatus=%%r
)
echo [ %ServiceStatus% ] %TmpServiceName%
goto :EOF
:: Help screen:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:About
ECHO.
ECHO SERVICE.CMD 1.02
ECHO Trying to write a linux like service command
ECHO Written by John Clark (johnrclark3@gmail.com)
ECHO.
ECHO Usage: SERVICE.CMD /? ^| /help ^| --help
ECHO SERVICE.CMD --status-all ^| service_name {info^|start^|stop^|restart^|status^|info^|disable^|enable}
ECHO.
ECHO Example: SERVICE WinRM stop
GOTO :END
:Syntax
ECHO Usage: SERVICE.CMD --help ^| --status-all ^| service_name {start^|stop^|restart^|status^|info^|disable^|enable}
GOTO :END
:: Cleanup on isle 3
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Housekeeping
SET ServiceName=
SET ServiceCommand=
SET ServiceStatus=
SET TmpServiceName=
SET FullServiceName=
SET Description=
goto :EOF
:END
call :Housekeeping
EXIT /B
@ECHO OFF
::
:: Better than "netsh firewall set opmode enable" ?
::
:: Command line error check:
IF /I "%1"=="" GOTO Syntax
GOTO Firewall%ERRORLEVEL%
GOTO Syntax
:: Choose State
:FirewallSTART
:FirewallON
SC QUERY mpssvc | FIND "STATE" | FIND "RUNNING" >nul
IF errorlevel 1 GOTO Start_Firewall
GOTO FirewallStarted
:Start_Firewall
sc config MpsSvc start= auto
sc start MpsSvc
GOTO :END
:FirewallSTOP
:FirewallOFF
SC QUERY mpssvc | FIND "STATE" | FIND "RUNNING" >nul
IF errorlevel 1 GOTO FirewallStopped
:Stop_Firewall
sc config MpsSvc start= disabled
sc stop MpsSvc
GOTO :END
:FirewallRESTART
SC QUERY mpssvc | FIND "STATE" | FIND "RUNNING" >nul
IF errorlevel 1 GOTO Start_Firewall
:Restart_Firewall
sc config MpsSvc start= auto
sc stop MpsSvc
sc start MpsSvc
GOTO :END
:: STATUS
:FirewallStarted
ECHO Firewall Started
GOTO END
:FirewallStopped
ECHO Firewall Stopped
:END
exit
:: Help screen:
:Syntax
ECHO.
ECHO SERVICE_FIREWALL.CMD 1.00
ECHO Starts and stops windows Firewall
ECHO Written by John Clark (johnrclark3@gmail.com)
ECHO.
ECHO Usage: SERVICE_FIREWALL.CMD <start>/<stop>
ECHO.
ECHO Examples: SERVICE_FIREWALL.CMD START
:End
ECHO.
for /f "tokens=1" %%i in ('wmic bios get serialnumber') do if not %%i==SerialNumber (
netdom renamecomputer %computername% /newname:PSY-%%i /userd:<domain>\admin /passwordd:* /reboot:20 /usero:administrator /passwordo:* /force
)
@echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
@echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
@echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
@cscript %temp%\sudo.tmp.vbs
@ECHO OFF
:: Command line error check:
IF "%1"=="" GOTO Syntax
cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell "Get-Content %~f1 -Wait"
exit
:: Help screen:
:Syntax
ECHO.
ECHO TAIL.CMD 1.00
ECHO Why isn't this in windows yet?
ECHO Written by John Clark (johnrclark3@gmail.com)
ECHO.
ECHO Usage: TAIL.CMD filename_name
ECHO.
ECHO You must specify filename_name with path and extension,
ECHO Spaces or wildcards aren't allowed.
ECHO.
ECHO The default WDS folder will be used. IE: C:\WDS\
ECHO.
ECHO Examples: TAIL.CMD c:\windows\setupact.log
:End
ECHO.
@takeown /f %1 /r /d y && icacls %1 /grant Administrators:F /t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment