[英]I'm a novice at Powershell and im trying to complete a task
我開始與 Powershell 合作,並且一直在在線學習一些課程。 此刻我陷入了挑戰,我希望一些好心人可以幫助我完成課程中的說明。
說明是:
1-找出服務器機器上安裝了哪些 Windows 功能。 (我正在將命令遠程發送到名為“服務器”的計算機)
2-僅選擇存儲在名稱和安裝狀態列中的數據。
3-按 Name 屬性按升序(字母順序)對數據進行排序。
4-確保 output 格式設置為表格
5-將最終的 output 保存到桌面計算機上名為 C:\features.txt 的文件中。
我想出的是:
Invoke-Command -ComputerName Server -ScriptBlock{
>> Get-WindowsFeature | Select-Object -Property Name, InstallState | Sort-Object -Property Name | Format-Table
>> } | Out-File C:\features.txt
我已經嘗試過使用和不使用 select-object 命令,因為我知道在這種情況下 format-table 命令的工作原理幾乎相同。 感謝你!
每當我在遠程計算機上調用腳本塊時,我都會將它分配給一個變量,然后這樣處理結果:
$myResults= Invoke-Command -ComputerName Server -ScriptBlock{…}
Foreach ($item in $myResults){
Write-host” $item.name /
$item.nstallState”
}
你過度使用管道,這並沒有錯,但對我個人來說,我自己還是一個初學者,要完全理解他們每個人到底在返回和轉發到下一個是不容易的。 不要試圖在第一次嘗試時寫出帶有幾個點的完美線。 從任務的最小最快部分開始。 在這種情況下,yust 在沒有過濾器和管道的情況下將它們全部取出。 然后從那里開始工作,做一些小的改變,使用 write-host 來顯示結果並自己跟蹤和錯誤以更深入地了解它們中的每一個。 然后最后你可以將它們鏈接起來。
例如,根據我的評論。
# 1-Find out what Windows features are installed on the server machine.
Get-WindowsFeature
# Results
<#
#>
# 2-Select only the data stored in the Name and InstallState columns.
Get-WindowsFeature |
Select-Object -Property Name, InstallState
# Results
<#
#>
# 3 - Sort the data by the Name property in ascending (alphabetical) order.
Get-WindowsFeature |
Select-Object -Property Name, InstallState |
Sort-Object -Property Name
# Results
<#
#>
# 4-Make sure the output format is set to table
Get-WindowsFeature |
Select-Object -Property Name, InstallState |
Sort-Object -Property Name |
Format-Table # though this is not needed, since table is the default for less than 5 properties.
# Results
<#
#>
<#
# 5-Save the final output into a file called C:\features.txt on your desktop
machine.
#>
Get-WindowsFeature |
Select-Object -Property Name, InstallState |
Sort-Object -Property Name |
Export-Csv -Path 'SomePathName' -NoTypeInformation -Append
# (I'm remoting command to a computer named "Server")
$Computers |
ForEach-Object {
Invoke-Command -ComputerName $PSItem.ComputerName -ScriptBlock{
Get-WindowsFeature |
Select-Object -Property Name, InstallState |
Sort-Object -Property Name |
Export-Csv -Path 'SomePathName' -NoTypeInformation -Append
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.