Powershell和System Center"您无法在空值表达式上调用方法

时间:2015-05-28 14:48:35

标签: powershell reboot sccm

我尝试创建一个在我的客户端计算机上运行的脚本,并检查挂起的重新启动。如果有挂起的重新启动,则会弹出一条消息,要求用户重新启动或推迟。当我在我的本地计算机上尝试它时,这很好用。但是,一旦我将它放入System Center并进行部署,它就不会运行并且我得到错误"警告:你不能在空值表达式上调用方法"有任何想法吗?这是脚本

[CmdletBinding()]
param(
    [Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [Alias("CN","Computer")]
    [String[]]$ComputerName="$env:COMPUTERNAME",
    [String]$ErrorLog
    )

Begin
    {
        # Adjusting ErrorActionPreference to stop on all errors, since using [Microsoft.Win32.RegistryKey]
        # does not have a native ErrorAction Parameter, this may need to be changed if used within another
        # function.
        $TempErrAct = $ErrorActionPreference
        $ErrorActionPreference = "Stop"
    }#End Begin Script Block
Process
    {
        Foreach ($Computer in $ComputerName)
            {
                Try
                    {
                        # Setting pending values to false to cut down on the number of else statements
                        $PendFileRename,$Pending,$SCCM = $false,$false,$false

                        # Setting CBSRebootPend to null since not all versions of Windows has this value
                        $CBSRebootPend = $NULL


                        # Querying WMI for build version
                        $WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber, CSName -ComputerName $Computer

                        # Making registry connection to the local/remote computer
                        $RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$Computer)

                        # If Vista/2008 & Above query the CBS Reg Key

                        If ($WMI_OS.BuildNumber -ge 6001)
                            {
                                $RegSubKeysCBS = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\").GetSubKeyNames()
                                $CBSRebootPend = $RegSubKeysCBS -contains "RebootPending"

                            }#End If ($WMI_OS.BuildNumber -ge 6001)

                        # Query WUAU from the registry
                        $RegWUAU = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
                        $RegWUAURebootReq = $RegWUAU.GetSubKeyNames()
                        $WUAURebootReq = $RegWUAURebootReq -contains "RebootRequired"

                        # Closing registry connection
                        $RegCon.Close()

                        # Determine SCCM 2012 Client Reboot Pending Status
                        # To avoid nested 'if' statements and unneeded WMI calls to determine if the CCM_ClientUtilities class exist, setting EA = 0
                        $CCMClientSDK = $null
                        $CCMSplat = @{
                            NameSpace='ROOT\ccm\ClientSDK'
                            Class='CCM_ClientUtilities'
                            Name='DetermineIfRebootPending'
                            ComputerName=$Computer
                            ErrorAction='SilentlyContinue'
                            }
                        $CCMClientSDK = Invoke-WmiMethod @CCMSplat
                        If ($CCMClientSDK)
                            {
                                If ($CCMClientSDK.ReturnValue -ne 0)
                                    {
                                        Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"

                                    }#End If ($CCMClientSDK -and $CCMClientSDK.ReturnValue -ne 0)

                                If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)
                                    {
                                        $SCCM = $true

                                    }#End If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)

                            }#End If ($CCMClientSDK)
                        Else
                            {
                                $SCCM = $null

                            }                        

                        # If any of the variables are true, set $Pending variable to $true
                        If ($CBSRebootPend -or $WUAURebootReq -or $SCCM)
                            {
                                $Pending = $true

                            }#End If ($CBS -or $WUAU)


                        If ($Pending = $CBSRebootPend -or $WUAURebootReq -or $SCCM)
                            {
                                $a = new-object -comobject wscript.shell
                                $b = $a.popup(“A Reboot is Pending, Press ""OK"" to reboot now or ""Cancel"" to reboot later.“,240,”CRISTA IT”,1)
                                if($b -eq 1) {
                                                Restart-Computer
                                            }

                                if($b -eq 2) {

                                #cancle was selected in the box. So should exit
                                exit(1)
                                            }


                            }

                    }#End Try

                Catch
                    {
                        Write-Warning "$Computer`: $_"

                        # If $ErrorLog, log the file to a user specified location/path
                        If ($ErrorLog)
                            {
                                Out-File -InputObject "$Computer`,$_" -FilePath $ErrorLog -Append

                            }#End If ($ErrorLog)

                    }#End Catch

            }#End Foreach ($Computer in $ComputerName)

    }#End Process

End
    {
        # Resetting ErrorActionPref
        $ErrorActionPreference = $TempErrAct
    }#End End

请注意:我没有完全创建这个脚本,但是在线使用了不同的免费脚本来创建它。

0 个答案:

没有答案