我正在嘗試使用 AzureAD 中計算機的 objectid 創建一個 csv 檔案,以便能夠匯入到一個組中。csv的格式需要是:
version:v1.0
[memberObjectIdOrUpn]
然后是計算機的 objectid 串列
$devices = Get-AzureADDevice -SearchString COMPUTERS
$devices = $devices.objectid | Select-Object @{n = '[memberObjectIdOrUpn]'; e = { $_ } }
$devices | Export-Csv -Path C:\temp\computers.csv -NoTypeInformation
這將為我提供一個串列,其中唯一的問題是需要在檔案頂部添加“version:v1.0”。
在匯出 csv 之前是否有某種方法可以添加它,或者我需要這樣做:
@("version:v1.0") (Get-Content "C:\temp\computers.csv") | Set-Content "c:\temp\computers.csv"
我只是覺得有一些更好的方法:)
uj5u.com熱心網友回復:
我認為這Convert-Csv可能更容易,因為它回傳一個字串陣列而不是寫入檔案。
# get the system line separator for convenientce
$newline = [Environment]::NewLine
# set up the file header
$header = ("version:v1.0{0}" -f $newline)
# get the device list (returns an array of strings)
$devices = Get-AzureADDevice -SearchString COMPUTERS
$devices = $devices.objectid | Select-Object @{n = '[memberObjectIdOrUpn]'; e = { $_ } } | ConvertTo-Csv -NoTypeInformation
# write the header and your content
Set-Content -Value ($headers ($devices -join $newline)) -Path C:\temp\computers.csv
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363107.html
