- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- Thomas Lee
- 201字
- 2021-07-02 18:16:01
How to do it...
Once you have created the logon script GPO, as shown previously, it's time to create the script:
- Using the ISE, open the script file you created in the introduction to this recipe and enter the logon script:
# Sales Group Logon Script.ps1 # Logon Script for Sales Group to add printer # 1. Start transcript Start-Transcript -Path C:\transcript\transcript.txt
-Append # 2. Log information to the transcript '*** Logon script - Sales GVroup Logon Script.ps1' '*** Date/time: [{0}]' -f (Get-Date) # 3. Setup up printer connection then try to connect $Connection = 'PSRV\SGCP1' Try { $Printer = Get-Printer -Name $Connection If ($Printer) { '*** Sales group printer found' $Printer } Else {Throw "Printer not found"} } Catch { '*** SG Printer does not exist' '*** Date/time: [{0}]' -f (Get-Date) '*** Adding SG Printer ' Add-Printer -ConnectionName $connection -Verbose Get-Printer -Name $Connection '******';'' } # 5. And stop the transcript Stop-Transcript
- Once you create and save the script, you can test it by logging onto the SG-CL1 computer and displaying the file C:\Transcript\Transcript.txt, which on the first logon looks like this:
- Once the logon script has installed the printer, subsequent logon scripts create a transcript entry that looks like this: