![](/img/trans.png)
[英]Import Users into AD Using CSV & PowerShell - ExtensionAttribute2 Issue
[英]Import CSV issue powershell
我正在研究一个脚本来自动添加用户。 我想从目录中的所有CSV文件中读取文件,直到完成,然后再将这些csvs移至存档文件中。 在程序中,我正在检查txt文件,以确保该程序的另一个实例尚未运行。 之后,我允许代码运行或程序结束。 当我运行带有一个或两个名为test.csv和test2.csv的测试文件的程序时,我没有任何错误。 一旦我添加了随机名称.csv并尝试通过它们运行它们,我将得到一个错误,即import-csv无法找到该文件,并且路径是从脚本位置告诉我该文件不正确。
这是我的代码:
$fileDirectory = "c:\test";
$CheckRun = Get-Content c:\test\checkrun.txt
Write-Host $($CheckRun)
if($CheckRun -eq "notrunning") {
Get-ChildItem $fileDirectory -Filter *.csv | Foreach-Object {
$list= Import-Csv $_
$State = "running"
$basename = $_.BaseName
$file = $FileDirectory + "\" + $basename + ".csv"
$ArchiveFile = $fileDirectory + "\archive\" + $basename + "old.csv"
foreach ($entry in $list) {
# will run command here using the entries from the CSVs
write-Host $($entry.EMAIL) $($entry.FIRSTNAME) $($entry.LASTNAME) $($entry.PASSWORD)
}
Move-Item $($file) ${ArchiveFile} -force
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$State > c:\test\Checkrun.txt
}
$State = "notrunning"
$State > c:\test\Checkrun.txt
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
else {
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
当您获得子项时,您将获得一个文件对象。 尝试引用全名,而不是它对我有用:
Get-ChildItem $fileDirectory -Filter *.csv | Foreach-Object {
$list= Import-Csv $_.fullname
否则,它将尝试查找正在运行文件脚本的路径,而不是C:\\ test \\ filename.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.