I am trying to configure wsus downstream server on windows 2016 & 2012 machines with the below script but unable to invoke values in the variable (upstream server IP and port number).
These are the variables
$susdaywus='susday2555.'
$portnumber='8530'
$vmsautodeploy=$inputfile |?{$_.serverroles -eq 'WUS'}
#Install-WindowsFeature -Name UpdateServices, UpdateServices-WidDB, UpdateServices-Services, UpdateServices-RSAT, UpdateServices-API, UpdateServices-UI
$wusfeatureinstall=@'
Install-WindowsFeature -Name UpdateServices, UpdateServices-WidDB, UpdateServices-Services, UpdateServices-RSAT, UpdateServices-API, UpdateServices-UI
'@
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell -ScriptText $wusfeatureinstall
$susdaywus='susday2555.corp.ncr.com'
$portnumber='8530'
$wsusdownstreamserversetup= @'
$erroractionpreference="Continue"
$error.clear()
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
$wsusConfig = $wsus.GetConfiguration()
$wsusConfig.SyncFromMicrosoftUpdate=$false
$wsusConfig.UpstreamWsusServerName= $susdaywus
$wsusConfig.UpstreamWsusServerPortNumber=$portnumber
$wsusConfig.IsReplicaServer=$True
$wsusConfig.TargetingMode="Client"
$wsusConfig.Save()
$wsusSub = $wsus.GetSubscription()
$wsusSub.SynchronizeAutomatically=$True
$wsusSub.SynchronizeAutomaticallyTimeOfDay="12:00:00"
$wsusSub.NumberOfSynchronizationsPerDay="1"
$wsusSub.Save()
$wsusSub.StartSynchronization()
'@
$wsusdownstreamserversetupSub = $ExecutionContext.InvokeCommand.ExpandString($wsusdownstreamserversetup)
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell -ScriptText
The issue is that the ExpandString method replaces all variables in your text, including $wsus, $wsusConfig and $wsusSub.
And since they are not defined, you end up with the variables being replaced with an empty string.
To avoid variables to be expanded, you will have to place a back-tick in front of the dollar sign.
Something like this
$vmsautodeploy=$inputfile |?{$_.serverroles -eq 'WUS'}
$wusfeatureinstall=@'
Install-WindowsFeature -Name UpdateServices, UpdateServices-WidDB, UpdateServices-Services, UpdateServices-RSAT, UpdateServices-API, UpdateServices-UI
'@
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell -ScriptText $wusfeatureinstall
$susdaywus='susday2555.corp.ncr.com'
$portnumber='8530'
$wsusdownstreamserversetup= @'
$erroractionpreference="Continue"
$error.clear()
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
`$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
`$wsusConfig = `$wsus.GetConfiguration()
`$wsusConfig.SyncFromMicrosoftUpdate=$false
`$wsusConfig.UpstreamWsusServerName= '$susdaywus'
`$wsusConfig.UpstreamWsusServerPortNumber=$portnumber
`$wsusConfig.IsReplicaServer=`$True
`$wsusConfig.TargetingMode="Client"
`$wsusConfig.Save()
`$wsusSub = `$wsus.GetSubscription()
`$wsusSub.SynchronizeAutomatically=`$True
`$wsusSub.SynchronizeAutomaticallyTimeOfDay="12:00:00"
`$wsusSub.NumberOfSynchronizationsPerDay="1"
`$wsusSub.Save()
`$wsusSub.StartSynchronization()
'@
$wsusdownstreamserversetupSub = $ExecutionContext.InvokeCommand.ExpandString($wsusdownstreamserversetup)
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The issue is that the ExpandString method replaces all variables in your text, including $wsus, $wsusConfig and $wsusSub.
And since they are not defined, you end up with the variables being replaced with an empty string.
To avoid variables to be expanded, you will have to place a back-tick in front of the dollar sign.
Something like this
$vmsautodeploy=$inputfile |?{$_.serverroles -eq 'WUS'}
$wusfeatureinstall=@'
Install-WindowsFeature -Name UpdateServices, UpdateServices-WidDB, UpdateServices-Services, UpdateServices-RSAT, UpdateServices-API, UpdateServices-UI
'@
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell -ScriptText $wusfeatureinstall
$susdaywus='susday2555.corp.ncr.com'
$portnumber='8530'
$wsusdownstreamserversetup= @'
$erroractionpreference="Continue"
$error.clear()
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
`$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
`$wsusConfig = `$wsus.GetConfiguration()
`$wsusConfig.SyncFromMicrosoftUpdate=$false
`$wsusConfig.UpstreamWsusServerName= '$susdaywus'
`$wsusConfig.UpstreamWsusServerPortNumber=$portnumber
`$wsusConfig.IsReplicaServer=`$True
`$wsusConfig.TargetingMode="Client"
`$wsusConfig.Save()
`$wsusSub = `$wsus.GetSubscription()
`$wsusSub.SynchronizeAutomatically=`$True
`$wsusSub.SynchronizeAutomaticallyTimeOfDay="12:00:00"
`$wsusSub.NumberOfSynchronizationsPerDay="1"
`$wsusSub.Save()
`$wsusSub.StartSynchronization()
'@
$wsusdownstreamserversetupSub = $ExecutionContext.InvokeCommand.ExpandString($wsusdownstreamserversetup)
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Its failing!
Executed as below:
$wsusdownstreamserversetup= @'
`$erroractionpreference="Continue"
`$error.clear()
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
`$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
`$wsusConfig =`$wsus.GetConfiguration()
`$wsusConfig.SyncFromMicrosoftUpdate=`$false
`$wsusConfig.UpstreamWsusServerName=$($susdaywus)
`$wsusConfig.UpstreamWsusServerPortNumber=$($portnumber)
`$wsusConfig.IsReplicaServer=`$True
`$wsusConfig.TargetingMode="Client"
`$wsusConfig.Save()
`$wsusSub = `$wsus.GetSubscription()
`$wsusSub.SynchronizeAutomatically=`$True
`$wsusSub.SynchronizeAutomaticallyTimeOfDay="12:00:00"
`$wsusSub.NumberOfSynchronizationsPerDay="1"
`$wsusSub.Save()
`$wsusSub.StartSynchronization()
'@
$wsusdownstreamserversetupSub = $ExecutionContext.InvokeCommand.ExpandString($wsusdownstreamserversetup)
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell -ScriptText $wsusdownstreamserversetupSub
ScriptOutput:
You have to put that string between quotes, otherwise PS will think it is a cmdlet or function.
Btw, there is no need to use $($susdaywus) here.
`$wsusConfig.UpstreamWsusServerName= '$susdaywus'
Update:you also have to escape the $ sign in $true
I updated the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Its working
Hi LucD,
I am sorry when I tested on one machine it showed no error but I started executing on couple of machines. Script failing
Is there anything wrong in the code itself.
here is the script
$wsusdownstreamserversetup= @'
`$erroractionpreference="Continue"
`$error.clear()
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
`$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
`$wsusConfig =`$wsus.GetConfiguration()
`$wsusConfig.SyncFromMicrosoftUpdate=`$false
`$wsusConfig.UpstreamWsusServerName='$($vmsautodeploy.UpStreamWUSServer)'
`$wsusConfig.UpstreamWsusServerPortNumber='$($vmsautodeploy.UpStreamWUSServerPort)'
`$wsusConfig.IsReplicaServer=`$True
`$wsusConfig.TargetingMode="Client"
`$wsusConfig.Save()
`$wsusSub = `$wsus.GetSubscription()
`$wsusSub.SynchronizeAutomatically=`$True
`$wsusSub.SynchronizeAutomaticallyTimeOfDay="12:00:00"
`$wsusSub.NumberOfSynchronizationsPerDay="1"
`$wsusSub.Save()
`$wsusSub.StartSynchronization()
'@
$wsusdownstreamserversetupSub = $ExecutionContext.InvokeCommand.ExpandString($wsusdownstreamserversetup)
Invoke-VMScript -VM $vmsautodeploy.VMName -GuestUser $vmsautodeploy.UserName -GuestPassword $vmsautodeploy.Password -ScriptType Powershell -ScriptText $wsusdownstreamserversetupSub
}
Error:
I'm not experienced a lot with WSUS, but I found quite a few mentions of that same error.
It seems they call that method with the WSUS server and the portnumber.
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusserver,$False,$portNumber)
I also read that you need to have WSUS Administration Console installed on the machine in order the have the required assemblies available.
Can you test your script directly on one of those machines?
As far as I can tell this is not a problem with Invoke-VMScript, but looks like a WSUS problem.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for your suggestion. I will test it directly.