2013-02-15 81 views
5

我是Powerhell的新手,需要帮助。我的第一个工作脚本是使AD环境中的新用户和被称为用户自动化。Powershell - 组合阵列

CSV转储将从我们的Peoplesoft系统每天完成一次。我使用Import-CSV并创建3个数组(新的,术语和处理)。

我遇到的麻烦是将3个数组组合在一起,我通过所有用户循环并尝试将其放回到文件中。代码在$New += $Term行中断。我相信这是由于我的测试文件中每个用户类型(新,术语和处理)只有1条记录(我知道,增加更多用户,不能,这对于任何用户来说都是真实世界的结果特定的日子)。以下是我的示例代码:

#Get Credentials from user 
$c = Get-Credential 

#Get Date for $Term array population 
$e = Get-Date -format M/d/yyyy 

#Set file location and variable for said file 
$File = "c:\users\nmaddux\desktop\adduserstuff\test.csv" 

#Import record sets for New and Term users 
$New = @() 
$Term = @() 
$Procd = @() 
$New = Import-Csv $File | Where-Object { 
      $_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq "" 
     } 
$Term = Import-Csv $File | Where-Object { 
      $_.TermDate -ne "" -and $_.Processdate -eq "" -and $_.TermDate -le $e 
     } 
$Procd = Import-Csv $File | Where-Object { $_.Processdate -ne "" } 

#Process both new and term users provided there are records to process for each 
If ($New -ne $NULL -and $Term -ne $NULL) { 
    # Some code to process users 
} 

$new += $term 
$new += $Procd 
$new | Export-Csv $file -NoTypeInformation -ErrorAction SilentlyContinue 

所以它会导出,但只有部分结果。

错误 - 方法调用失败,因为[System.Management.Automation.PSObject]不包含名为'op_Addition'的方法。

+1

'$ New + = $ Term'那条线是哪里......? – aquinas 2013-02-15 21:57:32

+0

添加了代码和错误声明......我的歉意。 – user2077056 2013-02-15 23:18:03

回答

11

如果Import-Csv只返回1个结果,那么你认为你的变量被认为不是一个数组,那么连接将失败。这并不是因为你已经用@()预先初始化了你的变量。实际上,这一步并非必要。

要强制将结果视为一个数组,您可以将整个Import-Csv行包装在@()中,或者在之后做类似的事情。

$new = @(Import-Csv $File | Where-Object {...}) 
# or 
$new = Import-Csv $File | Where-Object {...} 
$new = @($new) 
+0

非常好,谢谢!我会尝试。 附注,我试图把它全部发送到一个新的阵列....即 '$ total = @()' $ total + = $ new $ total + = $ term 然后导出$总共 它返回了我测试文件中的所有三个用户。为什么这个工作,而不是原来的? – user2077056 2013-02-15 23:13:23

+1

在这种情况下,您将'$ total'初始化为一个数组,然后立即对其进行“加 - 平等”操作。所以PowerShell知道你正在做一个数组操作,并采取适当的行动。在您的原始代码中,您将'$ new'初始化为一个数组,但在执行plus-equals之前将其重新分配给cmdlet的输出。如果cmdlet只返回1个值,那么powershell会将'$ new'更改为标量变量,然后加上等于没有意义。 – latkin 2013-02-16 00:04:32

+1

'$ a = @($ b)+ @($ c)'为我解决了类似的问题。感谢您的有用答案@latkin。 – kevinmicke 2014-01-31 21:30:14

0

您还可以通过类型转换变量执行类型:

$array = @() 
$array = gci test.txt 
$array.GetType() 

[array]$array = @() 
$array = gci test.txt 
$array.GetType() 

IsPublic IsSerial名称BASETYPE
-------- -------- --- - --------
真真FileInfo的System.IO.FileSystemInfo
真真对象[]的System.Array

1

所以要导入同一CSV˚F ile 3次?将它导入一次然后将数组设置为过滤它的“视图”不是更好吗?

有点像这样。您还应该能够使用每个数组中的“Count”值来说明是否返回了一个或多个结果。

#Get Credentials from user 
$c = Get-Credential 

#Get Date for $Term array population 
$e = Get-Date -format M/d/yyyy 

#Set file location and variable for said file 
$File = "c:\users\nmaddux\desktop\adduserstuff\test.csv" 

#Import record sets for New and Term users 
[array]$New 
[array]$Term 
[array]$Procd 

[array]$Import = Import-Csv $File 
[array]$New = $Import | ? {$_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq ""} 
[array]$Term = $Import | ? {$_.TermDate -ne "" -and $_.Processdate -eq "" -and $_.TermDate -le $e} 
[array]$Procd = $Import | ? {$_.Processdate -ne ""} 

#Process both new and term users provided there are records to process for each 
if (($New.Count -gt 0) -and ($Term.Count -gt 0)) 
{ 
    # Some code to process users 
} 

$new += $term 
$new += $Procd 
$new | Export-Csv $file -NoTypeInformation -ErrorAction SilentlyContinue 
0

我知道我来到这个讨论较晚,但别人说走来...... 既然你已经定义$新的空数组,当您从CSV导入要添加输出到您的预定义数组,不会将其设置为等于import-csv的输出。

$new = @() 
$new += Import-Csv $File | Where-Object { 
      $_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq "" 
     }