如何结合两个功能?

时间:2017-09-28 09:26:08

标签: powershell

有两个功能:

  • 检查PC是否正在等待重启
  • 检查wuauserv是否正在运行 - 如果它已停止为空$env:SystemRoot\SoftwareDistribution\Download文件夹。

如何在一个函数中将这两者结合起来执行两者,如果每个检查都为真,则为空$env:SystemRoot\SoftwareDistribution\Download

#function 1
If (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -ErrorAction Ignore)
{
    return $true
}
If (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction Ignore)
{
    return $true
}
If (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -ErrorAction Ignore)
{
    return $true
}
try
{ 
    $util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
    $status = $util.DetermineIfRebootPending()
    If (($status -ne $null) -and $status.RebootPending)
    {
        return $true
    }
} catch{}
return $false

#function 2
$getservice = Get-Service -Name wuauserv
while($getservice.Status -eq 'Running')
{
    Start-Sleep -s 1800
    $getservice = Get-Service -Name wuauserv
}

Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution\Download -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue"

2 个答案:

答案 0 :(得分:0)

我已使用WaitForStatus方法而非while循环,详情请参阅:http://www.powershellmagazine.com/2013/04/10/pstip-wait-for-a-service-to-reach-a-specified-status/

function Get-RebootPendingStatus {
    try
    {
        $util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
        $status = $util.DetermineIfRebootPending()
    } catch{}

    If (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -ErrorAction Ignore)
    {
        return $true
    }
    ElseIf (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction Ignore)
    {
        return $true
    }
    ElseIf (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -ErrorAction Ignore)
    {
        return $true
    }
    ElseIf (($status -ne $null) -and $status.RebootPending)
    {
        return $true
    }
    Else {
        return $false
    }
}

function Watch-ForServiceStatus ($ServiceName,$ServiceStatus) {
    $service = Get-Service -Name $ServiceName
    $service.WaitForStatus($ServiceStatus)
    return $true
}

$RebootStatus = Get-RebootPendingStatus
$ServiceStatus = Watch-ForServiceStatus -ServiceName wuauserv -ServiceStatus Stopped

If (($RebootStatus -eq $true) -and ($ServiceStatus-eq $true)) {
    Get-ChildItem -Path "$env:SystemRoot\SoftwareDistribution\Download" -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
}

答案 1 :(得分:0)

嗯,对于初学者,你必须定义功能。将注释作为函数1和函数2是不够的。把这些东西放在真正的函数中,并将它包含在一个更大的函数中,以满足您的需要。我不打算为你编写代码,但我会给你逻辑。

#Define single function (Dont mind the weird names)
Function EmptySoftFolder-IfconditionsAreTrue
{
    Function Check-RebootPendingStatus
    {
       #Function1 code goes here.
    }

    Function Get-WUAServiceStatus
    {
       #Function2 code goes here
    }

    $Fn1 = Check-RebootPendingStatus
    $Fn2 = Get-WUAServiceStatus

    if ($Fn1 -and $Fn2)
    {
        #Empty the software distribution folder code goes here
    }
}
#Call the single function like this
EmptySoftFolder-IfconditionsAreTrue