Powershell 如何将脚本修改为硬编码|导出-csv c:\temp\filename.csv -notypeinformation" [英] Powershell How to Modify Script to Hardcode " | export-csv c:\temp\filename.csv -notypeinformation"
问题描述
我有这个很棒的脚本,用于生成文件夹列表,其中包含分配的安全组和每个组中的每个用户.
I have this awesome script I use to generate a list of folders with their assigned security groups and each user in each group.
当我运行它时,我输入 .\getfolderacls.ps1 -verbose |导出-csv c:\temp\filename.csv -notypeinformation
.
When I run it, I type .\getfolderacls.ps1 -verbose | export-csv c:\temp\filename.csv -notypeinformation
.
这很完美,但我想对 | 进行硬编码export-csv...
部分,这样我就可以在没有参数的情况下运行它(或者它们是参数?).
That works perfectly, but I'd like to hardcode the | export-csv...
part so that I can just run it without the arguments (or are they parameters?).
我试着简单地附加|export-csv c:\temp\test.csv -notypeinformation
到脚本底部,但会引发错误不允许空管道元素
.
I tried simply appending | export-csv c:\temp\test.csv -notypeinformation
to the bottom of the script, but that throws the error An empty pipe element is not allowed
.
脚本:
[CmdletBinding()]
Param (
[ValidateScript({Test-Path $_ -PathType Container})]
[Parameter(Mandatory=$false)]
[string]$Path
)
Write-Verbose "$(Get-Date): Script begins!"
Write-Verbose "Getting domain name..."
$Domain = (Get-ADDomain).NetBIOSName
Write-Verbose "Getting ACLs for folder $Path"
Write-Verbose "...and all sub-folders"
Write-Verbose "Gathering all folder names, this could take a long time on bigger folder trees..."
$Folders = Get-ChildItem -Path I:\foldername -Directory -Recurse -Depth 2
Write-Verbose "Gathering ACL's for $($Folders.Count) folders..."
ForEach ($Folder in $Folders)
{ Write-Verbose "Working on $($Folder.FullName)..."
$ACLs = Get-Acl $Folder.FullName | ForEach-Object { $_.Access | where{$_.IdentityReference -ne "BUILTIN\Administrators" -and $_.IdentityReference -ne "BUILTIN\Users" }}
ForEach ($ACL in $ACLs)
{ If ($ACL.IdentityReference -match "\\")
{ If ($ACL.IdentityReference.Value.Split("\")[0].ToUpper() -eq $Domain.ToUpper())
{ $Name = $ACL.IdentityReference.Value.Split("\")[1]
If ((Get-ADObject -Filter 'SamAccountName -eq $Name').ObjectClass -eq "group")
{ ForEach ($User in (Get-ADGroupMember $Name -Recursive | Select -ExpandProperty Name))
{ $Result = New-Object PSObject -Property @{
Path = $Folder.Fullname
Group = $Name
User = $User
FileSystemRights = $ACL.FileSystemRights
}
$Result | Select Path,Group,User,FileSystemRights
}
}
Else
{ $Result = New-Object PSObject -Property @{
Path = $Folder.Fullname
Group = ""
User = Get-ADUser $Name | Select -ExpandProperty Name
FileSystemRights = $ACL.FileSystemRights
}
$Result | Select Path,Group,User,FileSystemRights
}
}
Else
{ $Result = New-Object PSObject -Property @{
Path = $Folder.Fullname
Group = ""
User = $ACL.IdentityReference.Value
FileSystemRights = $ACL.FileSystemRights
}
$Result | Select Path,Group,User,FileSystemRights
}
}
}
}
Write-Verbose "$(Get-Date): Script completed!"
推荐答案
您的脚本输出是在 foreach
loop - ForEach ($Folder in$Folders) ...
(而不是通过 ForEach-Object
cmdlet,不幸的是,它也被别名为 foreach
).
Your script's output is being produced inside a foreach
loop - ForEach ($Folder in $Folders) ...
(as opposed to via the ForEach-Object
cmdlet, which, unfortunately, is also aliased to foreach
).
为了将foreach
循环的输出发送到管道,您可以将它包装在一个脚本块({ ... }
)中并调用它使用点源操作符 (.
).或者,使用调用运算符 (&
),在这种情况下,循环在子范围内运行em>.
In order to send a foreach
loop's output to the pipeline, you can wrap it in a script block ({ ... }
) and invoke it with the dot-sourcing operator (.
).
Alternatively, use the call operator (&
), in which case the loop runs in a child scope.
以下是简化示例:
# FAILS, because you can't use a foreach *loop* directly in a pipeline.
PS> foreach ($i in 1..2) { "[$i]" } | Write-Output
# ...
An empty pipe element is not allowed.
# ...
# OK - wrap the loop in a script block and invoke it with .
PS> . { foreach ($i in 1..2) { "[$i]" } } | Write-Output
[1]
[2]
注意:我使用Write-Output
作为一个示例 cmdlet,您可以通过管道传输到该cmdlet,仅用于本演示的目的.在您的情况下,您需要将 foreach
循环包装在 中.{ ... }
并跟随它 |导出-CSV ...
而不是 Write-Output
.
Note: I'm using Write-Output
as an example of a cmdlet you can pipe to, solely for the purpose of this demonstration. What's required in your case is to wrap your foreach
loop in . { ... }
and to follow it with | Export-Csv ...
instead of Write-Output
.
使用<代码>.{ ... } 或 &{ ... }
将循环内生成的输出发送到管道正在生成,也就是以流式方式 -(通常)发生在 cmdlet 生成的输出中.
Using . { ... }
or & { ... }
sends the output generated inside the loop to the pipeline as it is being produced, one by one, aka in streaming fashion - as (typically) happens with output produced by a cmdlet.
替代方案是使用$(...)
,子表达式运算符(或@(...)
,数组子表达式运算符,在这个场景中的作用相同),在这种情况下,循环输出作为一个整体收集在内存中,预先,在通过管道发送之前 - 这通常更快,但需要更多内存强>:
An alternative is to use $(...)
, the subexpression operator (or @(...)
, the array-subexpression operator, which works the same in this scenario), in which case the loop output is collected in memory as a whole, up front, before it is sent through the pipeline - this is typically faster, but requires more memory:
# OK - call via $(...), with output collected up front.
PS> $(foreach ($i in 1..2) { "[$i]" }) | Write-Output
[1]
[2]
拼写.{ ... }
在您的代码上下文中解决方案 - 添加的行标有 # !!!
注释(另请注意根据 Lee_Dailey 对问题):
To spell the . { ... }
solution out in the context of your code - the added lines are marked with # !!!
comments (also note the potential to improve your code based on Lee_Dailey's comment on the question):
[CmdletBinding()]
Param (
[ValidateScript({Test-Path $_ -PathType Container})]
[Parameter(Mandatory=$false)]
[string]$Path
)
Write-Verbose "$(Get-Date): Script begins!"
Write-Verbose "Getting domain name..."
$Domain = (Get-ADDomain).NetBIOSName
Write-Verbose "Getting ACLs for folder $Path"
Write-Verbose "...and all sub-folders"
Write-Verbose "Gathering all folder names, this could take a long time on bigger folder trees..."
$Folders = Get-ChildItem -Path I:\foldername -Directory -Recurse -Depth 2
Write-Verbose "Gathering ACL's for $($Folders.Count) folders..."
. { # !!!
ForEach ($Folder in $Folders)
{ Write-Verbose "Working on $($Folder.FullName)..."
$ACLs = Get-Acl $Folder.FullName | ForEach-Object { $_.Access | where{$_.IdentityReference -ne "BUILTIN\Administrators" -and $_.IdentityReference -ne "BUILTIN\Users" }}
ForEach ($ACL in $ACLs)
{ If ($ACL.IdentityReference -match "\\")
{ If ($ACL.IdentityReference.Value.Split("\")[0].ToUpper() -eq $Domain.ToUpper())
{ $Name = $ACL.IdentityReference.Value.Split("\")[1]
If ((Get-ADObject -Filter 'SamAccountName -eq $Name').ObjectClass -eq "group")
{ ForEach ($User in (Get-ADGroupMember $Name -Recursive | Select -ExpandProperty Name))
{ $Result = New-Object PSObject -Property @{
Path = $Folder.Fullname
Group = $Name
User = $User
FileSystemRights = $ACL.FileSystemRights
}
$Result | Select Path,Group,User,FileSystemRights
}
}
Else
{ $Result = New-Object PSObject -Property @{
Path = $Folder.Fullname
Group = ""
User = Get-ADUser $Name | Select -ExpandProperty Name
FileSystemRights = $ACL.FileSystemRights
}
$Result | Select Path,Group,User,FileSystemRights
}
}
Else
{ $Result = New-Object PSObject -Property @{
Path = $Folder.Fullname
Group = ""
User = $ACL.IdentityReference.Value
FileSystemRights = $ACL.FileSystemRights
}
$Result | Select Path,Group,User,FileSystemRights
}
}
}
}
} | Export-Csv c:\temp\test.csv -notypeinformation # !!!
Write-Verbose "$(Get-Date): Script completed!"
这篇关于Powershell 如何将脚本修改为硬编码|导出-csv c:\temp\filename.csv -notypeinformation"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!