Reputation: 95
I have a script which checks AppPool Status on the local IIS system. It takes inputs in the script command line if we want to exclude certain AppPool. If there is no exclusion list provided in the command line then it basically finds all appPool status of IIS
Now what we need is to execute the script remotely on a remote IIS server instead of a local IIS server over winRM.
The webAdministration module will only be present on the remote IIS server and not on the local server from where the script is getting executed.
So how do we bring in Invoke-Command for remotely executing multi-line script like this over Remote Server ?
#Pass semi colon separated argument to exclude from being monitored eg Default Web Site;WebBDC3 (no quotation)
if (!$args.count -eq 0){
$EA=$args.get(0).split(';')
}
Import-Module WebAdministration
$returnStateOK = 0
# $returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3
$statuses = @{
ok = @()
critical = @()
}
$criticalTitles = "";
$countCritical = 0;
$countOK = 0;
if (-Not ($EA)){
$ApplicationPoolsState = Get-WebAppPoolState | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator | % {
if ($_.value -ne "Started"){
$statuses.critical += $_.key
}
else{
$statuses.ok += $_.key
}
}
}
else{
[System.Collections.ArrayList]$ApplicationPoolsState = @()
Get-WebAppPoolState | % {
$count = $ApplicationPoolsState.add(@{($_.itemxpath -split ("'"))[1]="$($_.value)"})
}
foreach($h in $EA){
if ($($ApplicationPoolsState.keys).IndexOf($h) -ge 0){
$ApplicationPoolsState.RemoveAt($($ApplicationPoolsState.keys).IndexOf($h))
}
}
$ApplicationPoolsState | % getEnumerator | % {
if ($_.value -ne "Started"){
$statuses.critical += $_.key
}
else{
$statuses.ok += $_.key
}
}
}
$countCritical = $statuses.critical.length
$countOK = $statuses.ok.length
EDIT: Thanks Tony for your response
I was thinking on the following line
if (-Not ($EA)){
$ApplicationPoolsState = Get-WebAppPoolState | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator | % {
if ($_.value -ne "Started"){
$statuses.critical += $_.key
}
else{
$statuses.ok += $_.key
}
}
}
I just need to execute the following on remote server
Import-Module WebAdministration Get-WebAppPoolState
Rest can be executed in the local server.
Now can i just invoke-command for these two lines using
$so = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$ApplicationPoolsState = Invoke-Command -ComputerName ${Node.DNS} -UseSSL -ScriptBlock { Import-Module WebAdministration ; Get-WebAppPoolState } -SessionOption $so -Credential Server\User1 | % { return @{($_.itemxpath -split ("'"))[1]="$($_.value)" } } | % getEnumerator | % {
if ($_.value -ne "Started"){
$statuses.critical += $_.key
}
else{
$statuses.ok += $_.key
}
}
This way i will execute only 2 lines on remote get the data locally and process the rest of the code locally
Upvotes: 0
Views: 290
Reputation: 1826
You only have to define a scriptblock and pass it to the parameter -ScriptBlock
of the invoke-command
cmdlet, e.g.:
$scriptblock = {
#put your code in here
}
Invoke-Command -ComputerName [computername] -ScriptBlock $scriptBlock
Upvotes: 0