Invoke-Command不会保存到哈希表

时间:2016-04-04 13:28:55

标签: powershell variables hashtable invoke-command

我编写了下面的脚本来查询服务器列表并获取所有共享驱动器,然后在这些驱动程序上查询ACL的路径。这个脚本中的所有内容看起来都很完美但是我有一行失败了。我已经尝试了这个我能想到的,它仍然失败了。我可以将共享信息保存到哈希表中,但是当我尝试将ACL保存到哈希表时,我什么也得不到。问题接近脚本的底部。我有几个不同的选项,我正在搞乱,我不会执行#。有什么想法吗?

#Creates the C:\temp folder if it does not exist.
if ( -Not (Test-Path 'C:\temp' ))
{
Write-Host 'Creating temp folder'
New-Item -Path 'C:\temp' -ItemType Directory
}
$Shares = @()
$Servers = (Get-Content C:\temp\servers.txt)
$Path = "C:\temp\ACLs.csv"

#Builds Table
$Table = @()
$Record = @{
"ServerName" = ""
"Directory" = ""
"Owner" = ""
"FileSystemRights" = ""
"AccessControlType" = ""
"IdentityReference" = ""
"IsInherited" = ""
"InheritanceFlags" = ""
"PropogationFlags" = ""
}

$ShareTable = @()
$ShareRecord = @{
"ShareName" = ""
"SharePath" = ""
"ShareDescription" = ""
}

#Gets shares for each server. Defines share table properties.
Foreach ($Server in $Servers)
{
$Shares += Invoke-Command -ComputerName $Server {Get-WmiObject Win32_share}
}
#Takes information from each share and populates share table
Foreach ($Share in $Shares)
{
$ShareRecord."ServerName" = $Share.PSComputerName
$ShareRecord."ShareName" = $Share.Name
$ShareRecord."SharePath" = $Share | select path | %{$_.path.trim()}
$ShareRecord."ShareDescription" = $Share.Description
$sobjRecord = New-Object PSObject -property $ShareRecord
$ShareTable += $sobjrecord
}  
Foreach ($Item in $ShareTable)
{
$Record."Directory" = $Item.sharepath | %{$_.trimstart("Microsoft.PowerShell.Core\FileSystem::")}
$Record."ServerName" = $Item.ServerName
$Record."Share" = $Item.ShareName
$Record."ShareDescription" = $Item.ShareDescription
#$TempServer = $Item.Servername
#$TempPath = $Item.Sharepath
#$ACL = Invoke-Command -ComputerName $Item.ServerName -ScriptBlock { param($ACLF) Get-Acl -Path $ACLF } -ArgumentList $Item.sharepath
#$ACL = Invoke-Command -ComputerName $Item.Servername (Get-Acl -Path $item.sharepath).access
#Invoke-Command -ComputerName $TempServer ((Get-Acl -Path $item.sharepath).access)
           Invoke-Command -Computer $item.servername {get-acl -Path $temppath | select -expand access }
Foreach ($SecItem in $ACL.Access)
{
#$Record."Owner" = $Item.Owner
$Record."FileSystemRights" = $SecItem.FileSystemRights
$Record."AccessControlType" = $SecItem.AccessControlType
$Record."IdentityReference" = $SecItem.IdentityReference
$Record."IsInherited" = $SecItem.IsInherited
$Record."InheritanceFlags" = $SecItem.InheritanceFlags
$Record."PropogationFlags" = $SecItem.PropagationFlags
$objRecord = New-Object PSObject -property $Record
$Table += $objrecord
}  
}

$Table | sort Servername, Share | Export-Csv -Path $Path -NoTypeInformation

0 个答案:

没有答案