VMware Cloud Community
vin01
Expert
Expert
Jump to solution

configure wsus downstream server in vm using Invoke-VMScript

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

pastedImage_4.png

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
vin01
Expert
Expert
Jump to solution

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:

pastedImage_1.png

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

vin01
Expert
Expert
Jump to solution

Thanks Its working

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert
Jump to solution

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

}

pastedImage_4.png

Error:

pastedImage_0.png

pastedImage_1.png

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

vin01
Expert
Expert
Jump to solution

Thanks for your suggestion. I will test it directly.

Regards Vineeth.K
0 Kudos