locked
WSUS with PowerShell 2.0 RRS feed

  • Question

  • I have been writing the following script to retrieve information from WSUS 3.2 and am getting returned information with the last command under PowerShell 4.0

    However when this script is run under PowerShell 2.0 no information is returned

    [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
    $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();

    $approveState = 'Microsoft.UpdateServices.Administration.ApprovedStates' -as [type]
    $installableitem = 'Microsoft.UpdateServices.Administration.FileForInstallableItem' -as [type]
    $ApprovedFiles = @()
    $Removable=0
    $NotRemovable=0

    $updateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope -Property @{
        ApprovedStates = $approveState::LatestRevisionApproved#,NotApproved,Declined,HasStaleUpdateApprovals,LatestRevisionApproved
    }
    $Approved = $wsus.GetUpdates($updateScope)
    Write-Host Approved Update $Approved.count
    If ($Approved.count -GT 0) {
        $Counter = 0
        $Approved | ForEach-Object {
        $ApprovedFiles += $_.Getinstallableitems().files.fileuri.segments
        }
    }

    this is important as I am tiring to run this script on SBS 2011 server which can only run PowerShell 2.0.

    Any assistance with this or an equivalent command appreciated

    Thanks 

    Tuesday, August 11, 2015 10:52 AM

All replies

  • you've tried to install powershell 4 on SBS and it doesn't work? i think it should since it's based on the 2008 R2 code

    powershell 3 should work too

    have you ensured you're allowing unsigned scripts to run as well as elevated permissions?

    on what OS is this script working via powershell 4? I know 2012 and 2012 R2 have powershell cmdlets but no other OS below them has that

    • Proposed as answer by Steven_Lee0510 Monday, August 17, 2015 2:22 AM
    • Unproposed as answer by Steven_Lee0510 Tuesday, September 8, 2015 8:21 AM
    • Proposed as answer by Steven_Lee0510 Tuesday, September 8, 2015 8:21 AM
    • Marked as answer by Steven_Lee0510 Tuesday, September 8, 2015 8:21 AM
    • Unmarked as answer by Robert B Bruce Tuesday, September 8, 2015 8:40 AM
    • Unproposed as answer by Robert B Bruce Tuesday, September 8, 2015 8:45 AM
    Wednesday, August 12, 2015 7:03 PM
  • Hi,

    Sorry for the delay.

    I build a new lab for testing your script.

    It seems that your script works with my powershell 2.0. Here is the screenshot:

    Best Regards.


    Steven Lee Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Proposed as answer by Steven_Lee0510 Tuesday, September 8, 2015 8:21 AM
    • Unproposed as answer by Robert B Bruce Tuesday, September 8, 2015 8:45 AM
    Monday, August 17, 2015 2:21 AM
  • Sorry for the long delay in response.

    As you have shown above the script does run, however nothing is returned to the $ApprovedFiles. I have proven that the issue lies with PS2 as I upgraded the SBS server to PS4 and I ran perfectly, however this did break the integration between the SBS console and Exchange, etc. once PS4 was uninstalled the integration worked but not the script.

    Thanks

    Robert

    Tuesday, September 8, 2015 8:45 AM
  • you were right though, it does say under the system requirements section of the windows framework 4.0 download page that it should not be installed on SBS 2011

    it seems like you do need v4 for the script to function

    wondering if you can execute the script remotely from 2012 R2 to hook into the SBS server instead of running it locally

    Tuesday, September 8, 2015 2:26 PM
  • Hi Robert,

    >>however nothing is returned to the $ApprovedFiles

    I didn't notice it because $ApprovedFiles is not printed by default.

    After some dig, I found that the Segments element can't be accessed in that way in PS2.

    I modified your script and it works in my lab now:

    [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
     $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
    
    $approveState = 'Microsoft.UpdateServices.Administration.ApprovedStates' -as [type]
     $installableitem = 'Microsoft.UpdateServices.Administration.FileForInstallableItem' -as [type]
     $ApprovedFiles = @()
     $Removable=0
     $NotRemovable=0
    
    $updateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope -Property @{
         ApprovedStates = $approveState::LatestRevisionApproved#,NotApproved,Declined,HasStaleUpdateApprovals,LatestRevisionApproved
     }
     $Approved = $wsus.GetUpdates($updateScope)
     Write-Host Approved Update $Approved.count
     If ($Approved.count -GT 0) {
         $Counter = 0
         $Approved | ForEach-Object {
         $_.Getinstallableitems() | ForEach-Object {
         $_.files | select originuri | ForEach-Object {
         $ApprovedFiles += $_.originuri.segments
         }}}
     }

    Here is the screenshot of my lab:

    Best Regards.


    Steven Lee Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Proposed as answer by Steven_Lee0510 Wednesday, September 9, 2015 7:42 AM
    Wednesday, September 9, 2015 1:44 AM