powershell从循环中提取
我有下面的脚本powershell从循环中提取,powershell,Powershell,我有下面的脚本 Switch -Wildcard ("$($group)$($action)") { "Citrix*" {$GroupC = ("#GG-$CC--Citrix1","#GG-$CC-Citrix2-","#GG-$CC-Citrix3")} } If ($group -eq "Citrix"){ $Response = New-Object System.Collections.ArrayList $DateInFuture = Read-Host
Switch -Wildcard ("$($group)$($action)") {
"Citrix*" {$GroupC = ("#GG-$CC--Citrix1","#GG-$CC-Citrix2-","#GG-$CC-Citrix3")}
}
If ($group -eq "Citrix"){
$Response = New-Object System.Collections.ArrayList
$DateInFuture = Read-Host "How long user will be active (: in Days) "
ForEach ($GroupXc in $GroupC) {
Add-ADGroupMember -Credential $cred -Identity $GroupXc -Members $User.samaccountname -Verbose
}
}
我想用变量$DateInFuture groupXc凭据将结果保存在.csv中
我不确定如何在循环外提取GroupXc
任何简单的方法???这里没有太多的上下文,但这应该让你开始。您需要用对象填充数组或arraylist。这使得导出到csv文件非常容易 例如:
Switch -Wildcard ("$($group)$($action)") {
"Citrix*" {$GroupC = ("#GG-$CC--Citrix1","#GG-$CC-Citrix2-","#GG-$CC-Citrix3")}
}
$OutputFile = C:\Temp\SomeOutput.csv # Obviously you should change this...
$Response = New-Object System.Collections.ArrayList
If ($group -eq "Citrix")
{
$DateInFuture = Read-Host "How long user will be active (: in Days) "
ForEach ($GroupXc in $GroupC)
{ # Start loop
$Response.Add(
[PSCustomObject]@{
DateInFuture = $DateInFuture
GroupXc = $GroupXc
Credentials = $Credentials
} )
Add-ADGroupMember -Credential $cred -Identity $GroupXc -Members $User.samaccountname -Verbose
} # End loop
}
$Response | Export-Csv -Path $OutputFile -NoTypeInformation
显然,您必须考虑对象的填充方式和位置。你必须使它符合你的需要。这只是一个例子