WSUS Computers已导出:使用Powershell在导入中筛选该CSV文件。

huangapple go评论80阅读模式
英文:

WSUS Computers exported: Filter that CSV file with Powershell in import

问题

你好,我需要使用PowerShell筛选一个.csv文件。
我导出的信息如下:

Get-WsusComputer -ComputerTargetGroups "group" | Export-Csv -Path "C:\export.csv"

但Get-WsusComputer向该.csv文件中添加了比我需要的更多信息,我只想要"FullDomainName",以便将这些计算机添加到新的AD组。
因此,我需要在导入时仅筛选"FullDomainName"标题。我该怎么做?

我尝试过这样:

Import-Csv "C:\export.csv" -Delimiter "," | select "FullDomainName"

但这样做时,尝试打印导入的值时,导入的值看起来像这样:

@{FullDomainName=blablabla.de}

我对PowerShell相当陌生,但据我理解,导入后以这种方式使用它将不正确,无法将这些完整域名添加到新的AD组。

感谢您的帮助!

英文:

Hello I need to filter a .csv file with powershell.
I exported the information like this:

Get-WsusComputer -ComputerTargetGroups "group" | Export-Csv -Path "C:\export.csv"

but Get-WsusComputer adds more information into that .csv than I need, I only want the "FullDomainName" so I can add those computers to a new AD-Group.
Therefore I need to filter for only the header "FullDomainName" when importing. how do i do that?

I tried this:

Import-Csv "C:\export.csv" -Delimiter "," | select "FullDomainName"

but when doing that and trying to print out the imported values, the imported values look like this:

@{FullDomainName=blablabla.de}

Im quite new to powershell, but to my understanding that import would not be right this way to after that be used to add those fulldomainnames to a new AD-Group.

Thanks for your help!

答案1

得分: 0

假设 FullDomainName 包含目标计算机的完全限定域名(例如 computer01.child.domain.tld),循环遍历这些名称并将它们拆分为名称和域,然后像这样查询 AD:

$computerFQDNs = Import-Csv "C:\export.csv" -Delimiter "," | ForEach-Object {
    $name,$domain = $_.FullDomainName -split '\.',2
    $computerAccount = Get-ADComputer -Identity $name -Server $domain -ErrorAction SilentlyContinue
    if($computerAccount){
        # 找到计算机帐户,将其添加到组
        Add-ADGroupMember -Identity myGroup -Member $computerAccount
    }
    else {
        Write-Warning "无法在域 '$domain' 中找到计算机帐户 '$name'"
    }
}

-split '\.',2 操作将接受输入字符串并在第一个 . 处分割它(即 computer.domain.tld -> computerdomain.tld)。

英文:

Assuming the FullDomainName contains the FQDN of the target machines (eg. computer01.child.domain.tld), loop over the names and split them into a name and domain, then query AD like this:

$computerFQDNs = Import-Csv "C:\export.csv" -Delimiter "," |ForEach-Object {
    $name,$domain = $_.FullDomainName -split '\.',2
    $computerAccount = Get-ADComputer -Identity $name -Server $domain -ErrorAction SilentlyContinue
    if($computerAccount){
        # Computer account found, add to group
        Add-ADGroupMember -Identity myGroup -Member $computerAccount
    }
    else {
        Write-Warning "Unable to find computer account '$name' in domain '$domain'"
    }
}

The -split '\.',2 operation will take the input string and split it at the first . (ie. computer.domain.tld -> computer and domain.tld)

huangapple
  • 本文由 发表于 2023年7月3日 19:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定