Hi,
Some of you need to execute powsershell from a windows batch command.
But if you want to deploy it on several windows server, it could be usefull to put it in a job using embedded script mode.
Just to be sure deployment is similar on each node.
Even you can write a powershell script directly in a job, it could complex to launch it without some parameters.
Solution could be to write a windows batch script, creating powershell script locally, executing it, and deleleting it after.
Here example of script, looking for ghost sessions under citrix server :
@ECHO OFF
SETLOCAL
Echo Add-PSSnapin Citrix* > %USERPROFILE%\documents\ghost.ps1
Echo Write-Host Check Ghost Sessions >> %USERPROFILE%\documents\ghost.ps1
Echo $NbError=0 >> %USERPROFILE%\documents\ghost.ps1
Echo $searchError="MachineName" # String to search >> %USERPROFILE%\documents\ghost.ps1
Echo # >> %USERPROFILE%\documents\ghost.ps1
Echo $when = [DateTime]::Now - [TimeSpan]::FromMinutes(30) >> %USERPROFILE%\documents\ghost.ps1
Echo $Result = Get-BrokerConnectionLog -Filter {ConnectionFailureReason -eq 'connectiontimeout' -and BrokeringTime -gt $when} >> %USERPROFILE%\documents\ghost.ps1
Echo # >> %USERPROFILE%\documents\ghost.ps1
Echo # Result example >> %USERPROFILE%\documents\ghost.ps1
Echo # >> %USERPROFILE%\documents\ghost.ps1
Echo #BrokeringTime : 24/07/2020 15:10:59 >> %USERPROFILE%\documents\ghost.ps1
Echo #BrokeringUserName : HOLDING\ATOT >> %USERPROFILE%\documents\ghost.ps1
Echo #BrokeringUserUPN : a.toto@escorial-development.eu >> %USERPROFILE%\documents\ghost.ps1
Echo #ConnectionFailureReason : ConnectionTimeout >> %USERPROFILE%\documents\ghost.ps1
Echo #Disconnected : False >> %USERPROFILE%\documents\ghost.ps1
Echo #EndTime : 24/07/2020 15:14:41 >> %USERPROFILE%\documents\ghost.ps1
Echo #EstablishmentTime : >> %USERPROFILE%\documents\ghost.ps1
Echo #MachineDNSName : CITRIX01.domain.com >> %USERPROFILE%\documents\ghost.ps1
Echo #MachineName : DOMAIN\CTITRIX01 >> %USERPROFILE%\documents\ghost.ps1
Echo #MachineUid : 60 >> %USERPROFILE%\documents\ghost.ps1
Echo #Uid : 437219 >> %USERPROFILE%\documents\ghost.ps1
Echo # >> %USERPROFILE%\documents\ghost.ps1
Echo $NbError=( "$Result" ^| select-string -pattern "$searchError" -AllMatches).matches.count >> %USERPROFILE%\documents\ghost.ps1
Echo If ($NbError -eq 0) >> %USERPROFILE%\documents\ghost.ps1
Echo { >> %USERPROFILE%\documents\ghost.ps1
Echo # No string found >> %USERPROFILE%\documents\ghost.ps1
Echo Write-Host $Result >> %USERPROFILE%\documents\ghost.ps1
Echo Write-Host "No sessions found : $NbError" >> %USERPROFILE%\documents\ghost.ps1
Echo exit 0 >> %USERPROFILE%\documents\ghost.ps1
Echo } >> %USERPROFILE%\documents\ghost.ps1
Echo Else >> %USERPROFILE%\documents\ghost.ps1
Echo { >> %USERPROFILE%\documents\ghost.ps1
Echo # No string found >> %USERPROFILE%\documents\ghost.ps1
Echo Write-Host $Result >> %USERPROFILE%\documents\ghost.ps1
Echo Write-Host "Number of sessions found : $NbError" >> %USERPROFILE%\documents\ghost.ps1
Echo If ($NbError -gt 10) { exit 1 } Else { exit 0 } >> %USERPROFILE%\documents\ghost.ps1
Echo } >> %USERPROFILE%\documents\ghost.ps1
Echo # >> %USERPROFILE%\documents\ghost.ps1
powershell -ExecutionPolicy ByPass -Command "%USERPROFILE%\documents\ghost.ps1"
SET ExitCode=%errorlevel%
del /F %USERPROFILE%\documents\ghost.ps1
exit %ExitCode%
Small trick, to be sure pipe character is write in pwershell script, don't forget to add ctrl character before ^|
Embeded script executing Powershell
Embeded script executing Powershell
- Attachments
-
- ghosts.pdf
- (34.41 KiB) Downloaded 2922 times
- ghosts.pdf
- (34.41 KiB) Downloaded 2922 times