Archive

Archive for the ‘Windows Server OS’ Category

Powershell Script to Diff CSV Data

November 4, 2014 Leave a comment

This script can be used to search for differences in CSV files based on a cell value and export the rows that are different to a new file. The steps are as follows:

  1. Import file1 – the file that has data to diff against.
  2. Convert the variable to an array based on the cell value you want to diff against.
  3. Import file2 – the file you want to get the diff data from.
  4. Set the export file name.
  5. For each line in file2, if the cell value is not contained in the file1 array variable, export the line to the export file.

You can copy and paste the code below or you can download the script here.


$file1 = ipcsv “.\1.csv”
$file1 = [array]$file1.No
$file2 = ipcsv “.\2.csv”
$file3 = “.\3.csv”


$file2 | %{if ($file1 -notcontains $_.No){$_ | epcsv $file3 -append -notype} }

 

Notes:

This script is about as simple as I could make it. There are obviously a ton of things you can do with this functionality. The concept is provided to manipulate according to your needs.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.

Categories: Scripts Tags:

Script to Check or Set Remote Desktop Configuration

February 5, 2012 Leave a comment

This script can be used to audit and/or set the Remote Desktop configuration on all Windows servers from Windows 2003 thru Windows 2008 R2 and all Windows clients from Windows XP thru WIndows 7. The steps are as follows:

  1. Check current Remote Desktop setting and set the %RDCFG% variable.
  2. Review the current Remote Desktop setting.
  3. Query to change Remote Desktop setting based on the current configuration.
  4. Change Remote Desktop setting or exit script based on user choice
  5. Review Remote Desktop setting again to validate configuration change.

You can copy and paste the code below or you can download the script here.


@echo off
TITLE Check/Set Remote Desktop Configuration
COLOR 17

:RDCHK
FOR /F “skip=1 tokens=1″ %%g IN (‘wmic rdtoggle get allowtsconnections ^|find ” “‘) DO (set RDCFG=%%g)

:RDRVW
cls
echo.
echo Current Remote Desktop configuration
echo ————————————

:RDRVW
echo.
IF /I “%RDCFG%”==”1” (echo Remote Desktop connections are enabled.) ELSE (IF /I “%RDCFG%”==”0” (echo Remote Desktop connections are disabled.))
goto RDQRY

:RDQRY
echo.
IF /I “%RDCFG%”==”0” (goto RDCFG0) ELSE (IF /I “%RDCFG%”==”1” (goto RDCFG1))

:RDCFG0
SET RDENB=
SET /P RDENB=Enable Remote Desktop connections? [y/n/q to quit] %=%
IF /I “%RDENB%”==”y” (goto RDENB) ELSE (IF /I “%RDENB%”==”n” (goto END) ELSE (IF /I “%RDENB%”==”q” (goto END)))
IF /I NOT “%RDENB%”==”y” (IF /I NOT “%RDENB%”==”n” (IF /I NOT “%RDENB%”==”q” goto RDQRY0))

:RDCFG1
SET RDDIS=
SET /P RDDIS=Disable Remote Desktop connections? [y/n/q to quit] %=%
IF /I “%RDDIS%”==”y” (goto RDDIS) ELSE (IF /I “%RDDIS%”==”n” (goto END) ELSE (IF /I “%RDDIS%”==”q” (goto END)))
IF /I NOT “%RDDIS%”==”y” (IF /I NOT “%RDDIS%”==”n” (IF /I NOT “%RDDIS%”==”q” goto RDQRY0))

:RDQRY0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto RDRVW

:RDENB
echo.
echo Enabling Remote Desktop…
ping localhost -n 3 >NUL
wmic rdtoggle WHERE “ServerName=’%computername%'” CALL SetAllowTSConnections 1 >NUL
goto RDCHK

:RDDIS
echo.
echo Disabling Remote Desktop…
ping localhost -n 3 >NUL
wmic rdtoggle WHERE “ServerName=’%computername%'” CALL SetAllowTSConnections 0 >NUL
goto RDCHK

:END
echo.
echo Exiting script…
echo.
ping localhost -n 5 >NUL
EXIT

 

Notes:

The WMIC call to set the TS connections requires the WHERE clause to work. Since this script uses the ServerName property, you may replace %computername% with the name of a remote server. As long as your user account has permission to access and modify the server via WMI remotely you will be able to check or set the Remote Desktop configuration of the remote server.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.

Script to Check or Set Windows Firewall

February 4, 2012 Leave a comment

This script can be used to audit and/or set the Windows Firewall service on Windows 2003 Server and modify the firewall profile states on Windows 2008, Windows 2008 R2 and Windows 7. I will add Windows XP and Vista at a later date. The steps are as follows:

  1. Check operating system version and set the %OSVER% variable.
  2. If %OSVER% is not set, warn then exit the script.
  3. Review the current Windows Firewall service settings (all operating systems).
  4. Review the current Windows Firewall profile states(2008, 2008 R2 and Windows 7).
  5. Query to change the firewall settings; allow user to exit the script.
  6. If user chooses to change firewall settings, prompt to set Windows Firewall service.
  7. If operating system is NOT Windows 2003, prompt to set each firewall profile state.
  8. Review Windows Firewall settings again to validate configuration change.

You can copy and paste the code below or you can download the script here.


@echo off
TITLE Check/Set Windows Firewall Configuration
COLOR 17

:OSCHK
FOR /F “tokens=4-5 delims=, ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”2003” (set OSVER=%%g) ELSE (IF /I “%%g”==”2008” (IF “%%g%%h”==”2008R2” (set OSVER=%%g %%h) ELSE set OSVER=%%g)))
FOR /F “tokens=3 delims= ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”7” set OSVER=%%g)

IF /I “%OSVER%”==”” (goto OSWARN) ELSE (goto FWRVW)

:OSWARN
echo.
echo.
echo *************************************
echo WARNING
echo *************************************
echo.
echo This script is designed to run on the following
echo Windows operating systems only:
echo Servers – 2003, 2008, 2008 R2
echo Clients – Windows 7
echo.
goto END

:FWRVW
cls
echo.
echo Current firewall settings
echo ————————-

:SVCRVW
echo.
SET FWSVC1=
SET FWSVC2=
FOR /F “tokens=2 skip=2 delims=,” %%g IN (‘wmic service WHERE “DisplayName=’Windows Firewall'” get state /format:csv’) DO (set FWSVC1=%%g)
FOR /F “tokens=2 skip=2 delims=,” %%g IN (‘wmic service WHERE “DisplayName=’Windows Firewall'” get startmode /format:csv’) DO (set FWSVC2=%%g)
echo Service State: %FWSVC1%
echo Service Startup: %FWSVC2%

IF /I NOT “%OSVER%”==”2003” goto PRFRVW

:PRFRVW
echo.
FOR /F “tokens=2 delims= ” %%g IN (‘netsh advfirewall show domainprofile ^|find “State”‘) DO (set FWDOM=%%g)
FOR /F “tokens=2 delims= ” %%g IN (‘netsh advfirewall show privateprofile ^|find “State”‘) DO (set FWPRV=%%g)
FOR /F “tokens=2 delims= ” %%g IN (‘netsh advfirewall show publicprofile ^|find “State”‘) DO (set FWPUB=%%g)
echo Domain Profile: %FWDOM%
echo Private Profile: %FWPRV%
echo Public Profile: %FWPUB%

:FWQRY
echo.
SET FWQRY=
SET /P FWQRY=Do you need to change the firewall settings? (y/n/q to quit): %=%
echo.
IF /I “%FWQRY%”==”y” (goto FWSVC) ELSE (IF /I “%FWQRY%”==”n” (goto END) ELSE (IF /I “%FWQRY%”==”q” (goto END)))
IF /I NOT “%FWQRY%”==”y” (IF /I NOT “%FWQRY%”==”n” (IF /I NOT “%FWQRY%”==”q” goto FWQRY0))

:FWQRY0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto FWRVW

:FWSVC
cls
echo.
SET FWSVC=
echo Set Windows Firewall service startup type:
echo 1. Automatic
echo 2. Manual
echo 3. Disabled
SET /P FWSVC= %=%

IF /I “%FWSVC%”==”1” (goto SVCAUT) ELSE (IF /I “%FWSVC%”==”2” (goto SVCMAN) ELSE (IF /I “%FWSVC%”==”3” (goto SVCDIS)))
IF /I NOT “%FWSVC%”==”1” (IF /I NOT “%FWSVC%”==”2” (IF /I NOT “%FWSVC%”==”3” goto FWSVC0))

:FWSVC0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto FWSVC

:SVCAUT
wmic service WHERE “DisplayName=’Windows Firewall'” CALL ChangeStartMode Automatic >NUL
IF /I “%FWSVC1%”==”Stopped” (wmic service WHERE “DisplayName=’Windows Firewall'” CALL StartService >NUL)
IF /I NOT “%OSVER%”==”2003” (goto FWPRF) ELSE (goto FWRVW)

:SVCMAN
wmic service WHERE “DisplayName=’Windows Firewall'” CALL ChangeStartMode Manual >NUL
IF /I NOT “%OSVER%”==”2003” (goto FWPRF) ELSE (goto FWRVW)

:SVCDIS
wmic service WHERE “DisplayName=’Windows Firewall'” CALL ChangeStartMode Disabled >NUL
IF /I “%FWSVC1%”==”Running” (wmic service WHERE “DisplayName=’Windows Firewall'” CALL StopService >NUL)
IF /I NOT “%OSVER%”==”2003” (goto FWPRF) ELSE (goto FWRVW)

:FWPRF
IF /I NOT “%OSVER%”==”2003” (goto PRFDOM) ELSE (goto END)

:PRFDOM
cls
echo.
SET PRFDOM=
SET /P PRFDOM=Set Doman Profile on/off: (on/off/q to quit) %=%

IF /I “%PRFDOM%”==”on” (netsh advfirewall set domainprofile state on >NUL) ELSE (IF /I “%PRFDOM%”==”off” (netsh advfirewall set domainprofile state off >NUL) ELSE (IF /I “%PRFDOM%”==”q” (goto END)))
IF /I NOT “%PRFDOM%”==”on” (IF /I NOT “%PRFDOM%”==”off” (IF /I NOT “%PRFDOM%”==”q” goto PRFDOM0))
goto PRFPRV

:PRFDOM0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto PRFDOM

:PRFPRV
cls
echo.
SET PRFPRV=
SET /P PRFPRV=Set Private Profile on/off: (on/off/q to quit) %=%

IF /I “%PRFPRV%”==”on” (netsh advfirewall set privateprofile state on >NUL) ELSE (IF /I “%PRFPRV%”==”off” (netsh advfirewall set privateprofile state off >NUL) ELSE (IF /I “%PRFPRV%”==”q” (goto END)))
IF /I NOT “%PRFPRV%”==”on” (IF /I NOT “%PRFPRV%”==”off” (IF /I NOT “%PRFPRV%”==”q” goto PRFPRV0))
goto PRFPUB

:PRFPRV0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto PRFPRV

:PRFPUB
cls
echo.
SET PRFPUB=
SET /P PRFPUB=Set Public Profile on/off: (on/off/q to quit) %=%

IF /I “%PRFPUB%”==”on” (netsh advfirewall set publicprofile state on >NUL) ELSE (IF /I “%PRFPUB%”==”off” (netsh advfirewall set publicprofile state off >NUL) ELSE (IF /I “%PRFPUB%”==”q” (goto END)))
IF /I NOT “%PRFPUB%”==”on” (IF /I NOT “%PRFPUB%”==”off” (IF /I NOT “%PRFPUB%”==”q” goto PRFPUB0))
goto FWRVW

:PRFPUB0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto PRFPUB

:END
echo Exiting script…
echo.
ping localhost -n 5 >NUL
EXIT

 

Notes:

The script will only check/set the Windows Firewall service on Windows 2003 Server.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.

Script to Check or Set Time Zone

February 3, 2012 Leave a comment

This script can be used to audit and/or set the Time Zone on Windows 2003 Server, Windows 2008, Windows 2008 R2 and Windows 7.  I will add Windows XP and Vista at a later date. The steps are as follows:

  1. Check operating system version and set the %OSVER% variable.
  2. If %OSVER% is not set, warn then exit the script.
  3. Review the current system time zone setting.
  4. Query to change the time zone; allow user to exit the script.
  5. If user chooses to change the time zone, offer US time zones to select from.
  6. If user selects a time zone, run code based on %OSVER%.
  7. If user selects a different time zone, open date/time Control Panel to manually set.
  8. Review the time zone settings again to validate configuration change.

Windows 2008 requires the use of tzone.exe. You can download tzone.exe here.

You can copy and paste the code below or you can download the script here.


@echo off
TITLE Check/Set Time Zone Configuration
COLOR 17

:OSCHK
FOR /F “tokens=4-5 delims=, ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”2003” (set OSVER=%%g) ELSE (IF /I “%%g”==”2008” (IF “%%g%%h”==”2008R2” (set OSVER=%%g%%h) ELSE set OSVER=%%g)))
FOR /F “tokens=3 delims= ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”7” set OSVER=%%g)

IF /I “%OSVER%”==”” (goto OSWARN) ELSE (goto TZRVW)

:OSWARN
echo.
echo.
echo *************************************
echo WARNING
echo *************************************
echo.
echo This script is designed to run on the following
echo Windows operating systems only:
echo Servers – 2003, 2008, 2008 R2
echo Clients – Windows 7
echo.
goto END

:TZRVW
cls
echo.
echo The time zone on this machine is:
FOR /F “skip=1 tokens=*” %%g IN (‘wmic timezone get description ^|find ” “‘) Do (@echo %%g)
goto TZQRY

:TZQRY
echo.
SET TZQRY=
SET /P TZQRY=Do you need to change the Time Zone? (y/n/q to quit): %=%
echo.
IF /I “%TZQRY%”==”y” (goto TZSEL) ELSE (IF /I “%TZQRY%”==”n” (goto END) ELSE (IF /I “%TZQRY%”==”q” (goto END)))
IF /I NOT “%TZQRY%”==”y” (IF /I NOT “%TZQRY%”==”n” (IF /I NOT “%TZQRY%”==”q” goto TZQRY0))

:TZQRY0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto TZRVW

:TZSEL
cls
echo.
echo Which Time Zone should this machine have?
echo 1. Pacific Time
echo 2. Mountain Time
echo 3. Arizona
echo 4. Central Time
echo 5. Eastern Time
echo 6. Other

echo.
SET SYSTZ=
SET /P SYSTZ= %=%
IF /I “%SYSTZ%”==”1” (goto TZPAC) ELSE (IF /I “%SYSTZ%”==”2” (goto TZMTN) ELSE (IF /I “%SYSTZ%”==”3” (goto TZARZ) ELSE (IF /I “%SYSTZ%”==”4” (goto TZCEN) ELSE (IF /I “%SYSTZ%”==”5” (goto TZEAS) ELSE (IF /I “%SYSTZ%”==”6” (goto TZOTH))))))
IF /I NOT “%SYSTZ%”==”1” (IF /I NOT “%SYSTZ%”==”2” (IF /I NOT “%SYSTZ%”==”3” (IF /I NOT “%SYSTZ%”==”4” (IF /I NOT “%SYSTZ%”==”5” (IF /I NOT “%SYSTZ%”==”6” (goto TZSEL0))))))

:TZSEL0
echo.
echo You have made an invalid entry – please try again.
echo.
pause
goto TZSEL

:TZPAC
IF /I “%OSVER%”==”2003” (control timedate.cpl,,/Z Pacific Standard Time) ELSE (IF /I “%OSVER%”==”2008” (tzone -Zone Pacific Time) ELSE (IF /I “%OSVER%”==”2008R2” (tzutil /s “Pacific Standard Time”) ELSE (IF /I “%OSVER%”==”7” (tzutil /s “Pacific Standard Time”))))
goto TZRVW

:TZMTN
IF /I “%OSVER%”==”2003” (control timedate.cpl,,/Z Mountain Standard Time) ELSE (IF /I “%OSVER%”==”2008” (tzone -Zone Mountain Time) ELSE (IF /I “%OSVER%”==”2008R2” (tzutil /s “Mountain Standard Time”) ELSE (IF /I “%OSVER%”==”7” (tzutil /s “Mountain Standard Time”))))
goto TZRVW

:TZARZ
IF /I “%OSVER%”==”2003” (control timedate.cpl,,/Z US Mountain Standard Time) ELSE (IF /I “%OSVER%”==”2008” (tzone -Zone Arizona) ELSE (IF /I “%OSVER%”==”2008R2” (tzutil /s “US Mountain Standard Time”) ELSE (IF /I “%OSVER%”==”7” (tzutil /s “US Mountain Standard Time”))))
goto TZRVW

:TZCEN
IF /I “%OSVER%”==”2003” (control timedate.cpl,,/Z Central Standard Time) ELSE (IF /I “%OSVER%”==”2008” (tzone -Zone “Central Time (US & Canada)”) ELSE (IF /I “%OSVER%”==”2008R2” (tzutil /s “Central Standard Time”) ELSE (IF /I “%OSVER%”==”7” (tzutil /s “Central Standard Time”))))
goto TZRVW

:TZEAS
IF /I “%OSVER%”==”2003” (control timedate.cpl,,/Z Eastern Standard Time) ELSE (IF /I “%OSVER%”==”2008” (tzone -Zone Eastern Time) ELSE (IF /I “%OSVER%”==”2008R2” (tzutil /s “Eastern Standard Time”) ELSE (IF /I “%OSVER%”==”7” (tzutil /s “Eastern Standard Time”))))
goto TZRVW

:TZOTH
echo.
echo Control Panel will now open so you
echo can set the time zone manually.
start control timedate.cpl
echo.
pause
goto TZRVW

:END
echo Exiting script…
echo.
ping localhost -n 5 >NUL
EXIT

 

Notes:

Since each line of a batch file causes the whole file to be processed again until it reaches the next line, I put as much logic in a single line as possible. That means several nested IF statements where necessary.

The script will open the date/time Control Panel to modify the time zone on Windows 2003 Server.

Windows 2008 Server requires the use of tzone.exe to modify the time zone.

Windows 2008 R2 Server uses native tzutil.exe to modify the time zone.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.

Script to Find Operating System Version

February 2, 2012 2 comments

The code below will set a variable if the operating system is Windows 2003, Windows 2008, Windows 2008 R2 or Windows 7.  I will add Windows XP and Vista at a later date.

:OSCHK

FOR /F “tokens=4-5 delims=, ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”2003” (set OSVER=%%g) ELSE (IF /I “%%g”==”2008” (IF “%%g %%h”==”2008R2” (set OSVER=%%g%%h) ELSE set OSVER=%%g)))

FOR /F “tokens=3 delims= ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”7” set OSVER=%%g)

 

Notes:

Since each line of a batch file causes the whole file to be processed again until it reaches the next line, I put as much logic in a single line as possible. That means several nested IF statements where necessary.

The variable %OSVER% is set and can be used to process code in other parts of the script that depend on the operating system version. It is suggested to use another nested IF statement directly after this code that will take the script to the part of your file that runs code based on the operating system version.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.

Script to Configure Windows NTP Time Servers

January 6, 2012 3 comments

Here is a script to configure the NTP time server list on Windows servers. You can use the script to set the NTP time servers on domain controllers.

 

Windows Server 2008 NTP server and Windows 2008 R2 NTP server configuration:

:: Windows 2008 Server

net stop w32time

:: Configure time servers after the /manualpeerlist switch
w32tm /config /syncfromflags:manual /manualpeerlist:time.windows.com,time-a.nist.gov,time-b.nist.gov
w32tm /config /reliable:yes

net start w32time
w32tm /query /configuration

 

Windows 2003 Server NTP server configuration:

:: Windows 2003 Server

net stop w32time

:: Configure time servers after the /manualpeerlist switch
w32tm /config /manualpeerlist:time.windows.com,time-a.nist.gov,time-b.nist.gov /syncfromflags:MANUAL /reliable:yes
w32tm /config /update

net start w32time
w32tm /resync

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.