簡體   English   中英

腳本沒有從文本文件中挑選所有服務器

[英]Script is not picking all servers from the text file

$Servers = Get-Content "D:\Server\Servers.txt"
#Gathering Vars
$OSVersion = (Get-CimInstance Win32_OperatingSystem).version
$WarningPreference = 'SilentlyContinue'
$key = 'HKLM:\Software\Company\SCCMMetaData'
$StampCheck = (Get-ItemProperty -Path $key).PatchRelease

$data = foreach ($server in $Servers) {
    #Gathering OS Version & Normalization of data
    If ($OSVersion -like "10.*") { $OSName = "WINS2016-"; $KB = "KB4540670", "KB4538461", "KB4550929", "KB4556813", "KB4549949", "KB4550994", "KB4494175", "KB4540723" }
    Elseif ($OSVersion -like "6.0.*") { $OSName = "WINS08R1-"; $KB = "KB4534303", "KB4534312" }
    Elseif ($OSVersion -like "6.1.*") { $OSName = "WINS08R2-"; $KB = "KB4534314", "KB4539602", "KB4534310" }
    Elseif ($OSVersion -like "6.2.*") { $OSName = "WINS12R1-"; $KB = "KB4541510", "KB4550917", "KB4556840", "KB4540726" }
    Elseif ($OSVersion -like "6.3.*") { $OSName = "WINS12R2-"; $KB = "KB4541509", "KB4550961", "KB4556846", "KB4540725" }

    #Check to see if KB is installed & build the stamp
    Try {
        $KBSearch = Get-HotFix -id $KB -ErrorAction Stop
        $Stamp = $OSName 
        If ($StampCheck -eq "2020Q2") {  
            Return "$Server Already Compliant"
        }
        Else {
            Set-ItemProperty -path 'HKLM:\Software\Company\SCCMMetaData' -Name 'PatchRelease' -Value $Stamp
            Restart-Service -DisplayName 'Agent' -ErrorAction Ignore
            Start-Sleep 30
            Return "$Server Stamped"
        }
    }
    Catch { Return "Missing Patch $KB on server $Server" }
}

Restart-Service -DisplayName ' Agent'
$data | Export-Csv "D:\Scripts\KB.csv" -NoTypeInformation

這是我的代碼,它沒有對 .txt 文件中的所有服務器進行迭代。 它只占用列表中的第一個服務器。 它不會檢查 txt 文件中的每個服務器。 我在哪里做錯了? 任何人都可以幫助我嗎?

return將導致 PowerShell ......好吧,控制權返回給調用者:)

只需省略return關鍵字並保持它們后面的表達式原樣 - 無論如何它們都會自動“冒泡”給調用者:

    #Check to see if KB is installed & build the stamp
    Try {
        $KBSearch = Get-HotFix -id $KB -ErrorAction Stop
        $Stamp = $OSName 
        If ($StampCheck -eq "2020Q2") {  
            "$Server Already Compliant"
        }
        Else {
            Set-ItemProperty -path 'HKLM:\Software\Company\SCCMMetaData' -Name 'PatchRelease' -Value $Stamp
            Restart-Service -DisplayName 'Agent' -ErrorAction Ignore
            Start-Sleep 30
            "$Server Stamped"
        }
    }
    Catch { "Missing Patch $KB on server $Server" } 

您可以在about_Return幫助文件中閱讀有關return關鍵字的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM