简体   繁体   English

从CSV文件创建变量

[英]Create varibles from CSV file

My computerlist is a CSV file with 3 columns (Machinename, New_AU, and New_State). 我的计算机列表是一个包含三列(机器名称,New_AU和New_State)的CSV文件。

I'm trying to move each machine in my computer list to it's TargetOU. 我正在尝试将计算机列表中的每台计算机移至TargetOU。 The TargetOU for each machine is based on the New_AU and New_State. 每台计算机的TargetOU基于New_AU和New_State。 I'm not sure how to get this information from the CSV file. 我不确定如何从CSV文件中获取此信息。 Here is what I have so far. 这是我到目前为止所拥有的。 I appreciate any and all help on this matter. 感谢您在此问题上提供的所有帮助。

Import-Module ActiveDirectory

$ComputerList = Import-Csv -Path "C:\Temp\Scripts\AUchange\Computerlist.csv"
$TargetOU = "OU=$New_AU,OU=$New_State,OU=ST,OU=EN,DC=en,DC=wb,DC=bk,DC=cp"

foreach ($Computer in $ComputerList) {
    Get-ADComputer $Computer | 
        Move-ADObject -Server AD-server.en.wb.bk.cp -TargetPath $TargetOU
}

OK lets go over whats wrong. 好的,让我们来看看有什么问题。

The variables $New_AU and $New_State are not defined in script so right now there Null . 脚本中未定义变量$ New_AU$ New_State ,因此现在在Null

$ComputerList = Import-Csv -Path C:\Temp\Scripts\AUchange\Computerlist.csv
$TargetOU   =  "OU=$New_AU,OU=$New_State,OU=ST,OU=EN,DC=en,DC=wb,DC=bk,DC=cp"

Solution : I am going off the fact you said that your $ComputerList should be an Array of objects that contain multiable objects with the properties New_AU, New_State and MachineName 解决方案:我不赞成您说您的$ ComputerList应该是一个对象数组,其中包含具有属性New_AU,New_State和MachineName的多个对象

If thats the case you could use : 如果是这样,您可以使用:

Import-Module ActiveDirectory

Import-Csv -Path C:\Temp\Scripts\AUchange\Computerlist.csv | %{
    $TargetOU = "OU=$($_.New_AU),OU=$($_.New_State),OU=ST,OU=EN,DC=en,DC=wb,DC=bk,DC=cp"
    Get-ADComputer $_.Machinename | Move-ADObject -Server AD-server.en.wb.bk.cp -TargetPath $TargetOU
}

What is happening 怎么了

In powershell | 在powershell中| means Pipe. 表示管道。 This will send the data from the previous command to the next one in the pipe | 这会将数据从上一个命令发送到管道中的下一个命令 and the variable for that data will be $_ 该数据的变量为$ _

So we Import-CSV which turns the file into a Powershell Object. 因此,我们使用Import-CSV将文件转换为Powershell对象。 We then Pipe | 然后,我们| that object to a %{} which is shorthand for ForEach-Object 该对象指向%{} ,它是ForEach-Object的简写

We set the $TargetOU with a string using $() which means expression (It means run this section as script not as string. Then we call the object data passed from Import-CSV using $_ and then call each property needed $_.PropertyName . 我们使用$()$ TargetOU设置带有字符串的字符串,这表示表达式(这意味着将本节作为脚本而不是字符串运行。然后,使用$ _调用从Import-CSV传递的对象数据,然后调用所需的每个属性$ _。 PropertyName

We then Get-AdComputer using the piped | 然后,我们使用管道获取,AdComputer | data $_ with property MachineName that we got from Import-csv . Import-csv获得的具有属性MachineName的数据$ _。 We pipe that Get-AdComputer Object to Move-ADObject where we set the -TargetPath parameter to $TargetOU 我们将该Get-AdComputer对象传递给Move-ADObject ,在其中将-TargetPath参数设置为$ TargetOU

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM