简体   繁体   English

从变量中提取单个对象/属性(Powershell)

[英]Pulling individual objects/properties from variable (Powershell)

I'm currently storing AD user properties for multiple users in a variable. 我目前正在将多个用户的AD用户属性存储在一个变量中。 I need to then be able to pull out the individual properties to feed into an XML file. 然后,我需要能够提取各个属性以将其馈入XML文件。 Right now I'm somewhat stuck as I don't know if I can pull out the individual properties. 现在,我有点不知所措,因为我不知道是否可以提取单个属性。 I know the code as I have it tries to populate all the properties for each user in each variable in the XML, but wanted to show the thought process. 我知道代码,因为它试图填充XML中每个变量中每个用户的所有属性,但希望显示其思想过程。 Is there a way to call the individual properties? 有没有一种方法可以调用各个属性?

$base = 'ou=my users,dc=mydomain,dc=com'
$date = (Get-Date).AddDays(-1)

$people = get-aduser -SearchBase $base -Filter {whencreated -gt $date} -Properties * | select name,emailaddress,title -OutVariable results

foreach ($result in $results){

$name = $results.name
$email = $results.email
$title = $results.title

$xmlfile=[xml] @"
<users><user><name>$name</name><email>$email</email><title>$title</title</user></users>
"@

Will be much easier to do like this: 这样会容易得多:

$base = 'ou=my users,dc=mydomain,dc=com'
$date = (Get-Date).AddDays(-1)

$people = get-aduser -SearchBase $base -Filter {whencreated -gt $date} -Properties * | select name,emailaddress,title -OutVariable results

$xmlfile= $people | ConvertTo-Xml

Anyway your foreach section is wrong as well, you are calling foreach ($result in $results) which is not exist, your array called $people , also your email definition is wrong, should be emailaddress 无论如何,您的foreach部分也是错误的,您正在调用不存在的foreach ($result in $results) ,您的数组名为$people ,而且您的email定义错误,应为emailaddress

foreach ($result in $people){

$name = $result.name
$email = $result.emailaddress
$title = $result.title

[xml]$xmlfile...
}

To get the InnerXml: 要获取InnerXml:

$xmlfile.InnerXml

I was trying to find a way to use xml processes and methods as much as possible here to adhear to the proper methods of creating this file. 我试图在这里找到一种尽可能使用xml进程和方法的方法,以了解创建此文件的正确方法。 Hopefully someone has a better approach but it gets the results I think you want. 希望有人能有更好的方法,但是可以得到我认为您想要的结果。

# Base XML
$xml = [xml]@"
<users>
</users>
"@

# Template for creating nodes. 
$userTemplate = @"
<user>
    <name>{0}</name>
    <email>{1}</email>
    <title>{2}</title>
</user>
"@

$base = 'ou=my users,dc=mydomain,dc=com'
$date = (Get-Date).AddDays(-1)

Get-ADUser -SearchBase $base -Filter {whencreated -gt $date} -Properties emailaddress,title | select name,emailaddress,title| ForEach-Object{
    # Create and append a child for each user
    $child = $xml.ImportNode(([xml]($userTemplate -f $_.Name,$_.emailaddress,$_.title)).DocumentElement,$true)
    $xml.documentelement.AppendChild($child) | Out-Null
}

# Should use your own path and a variable here. 
$xml.Save("c:\temp\text.xml")

Your saved file should now look like this: 您保存的文件现在应如下所示:

<users>
  <user>
    <name>Matt</name>
    <email>address@mail.com</email>
    <title>Awesome</title>
  </user>
  <user>
    <name>Michael</name>
    <email>address2@mail.com</email>
    <title>Guy</title>
  </user>
</users>

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

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