问题描述
我正在尝试使用Powershell来获取服务器名称列表并列出c:\users
中的所有文件夹。
我最终希望将其写入文件,但是现在我只是想将它们列出到控制台中。
$m = import-csv -Path .\serverlist.csv
$m | Foreach-Object {
$flist = Invoke-Command -Computername $_ -ScriptBlock {
Get-Childitem -Path C:\users -Directory
}
write-host $flist
}
当我尝试运行脚本时,我得到了
Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI,use the -ConnectionUri parameter,or pass URI objects instead of strings.
At C:\Users\omurac\Desktop\scripts\TestSnip\ImportorEachObject.ps1:4 char:32
+ ... { $flist = Invoke-Command -Computername $_ -ScriptBlock {Get-Childit ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command],ArgumentException
+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand
如果我通过$ m变量进行写主机,它将显示以下列表:
Name= server1
name=server2
nAME= SERVER3
ETC ...
有没有办法确保它将csv中的项目作为字符串传递?
感谢您的帮助。
解决方法
我发现,除了全文解释之外,我还更容易用一些注释对它进行重新编码,以获得更好的示例:
# Import CSV. Consider if it has headers or not,you can use parameters to play with them. Lets imagine CSV is something like this:
# ServerName
# localhost
# localhost
# localhost
$servers = Import-Csv -Path .\serverlist.csv
# this goes thru each server read from the CSV. By default,first row is treated as header.
foreach ($line in $servers)
{
# When calling the computername,you refer the line,and to the particular column name. here we assume it is called "ServerName"
$result = Invoke-Command -Computername $line.ServerName -ScriptBlock {
Get-Childitem -Path C:\Users -Directory
}
# You used a write-host,what is intended for a string output.
# As you used a Get-ChildItem,you will get an array of values. Easy & Nice way to display it is using a Format-Table output
$result | Format-Table -AutoSize
}
谢谢。
,由于您已经有了计算机列表,所以最好像这样使用Invoke-Command
$serverlist = Import-Csv -Path .\serverlist.csv
$folderlist = Invoke-Command -ComputerName $serverlist.name -ScriptBlock {
Get-Childitem -Path C:\users -Directory
}
这将异步地对每台服务器运行(在Windows Powershell 5.1中默认为32台),而不是在foreach
循环中一次运行一台。
在默认情况下,返回的属性为PSComputerName
,以便您知道哪些文件夹属于哪个服务器。这是上述代码返回的默认成员的完整列表。
Name MemberType
---- ----------
GetType Method
ToString Method
BaseName NoteProperty
LinkType NoteProperty
Mode NoteProperty
PSChildName NoteProperty
PSComputerName NoteProperty
PSDrive NoteProperty
PSIsContainer NoteProperty
PSParentPath NoteProperty
PSPath NoteProperty
PSProvider NoteProperty
PSShowComputerName NoteProperty
RunspaceId NoteProperty
Target NoteProperty
Attributes Property
CreationTime Property
CreationTimeUtc Property
Exists Property
Extension Property
FullName Property
LastAccessTime Property
LastAccessTimeUtc Property
LastWriteTime Property
LastWriteTimeUtc Property
Name Property
Parent Property
Root Property
一旦您收集了$folderlist
中的信息,就可以查看,导出等。
我想指出/推荐的另一件事是变量的命名。忽略单数或复数变量非常容易,我们一直在看到这样的循环
foreach($username in $usernames){do something on $usernames}
,反之亦然,所以一种常见的做法是像在我的示例中一样,将它们描述性地命名。 $serverlist
和$folderlist
可以轻松查看其中的内容。