簡體   English   中英

如何將文本文件值放入 Invoke-VMScript 命令?

[英]How to get Text file values into an Invoke-VMScript command?

我正在嘗試使用 Invoke-VMScript 在新創建的 DC 中創建 OU,但我無法從文本文件中獲取 OU 值以推送到 VM。 我也不確定在哪里執行多行腳本中的中斷,以便它正確傳遞到 VM。

OU 文件示例:

Adm_Apps
Adm_Apps/Groups
Adm_Apps/Users
Adm_Apps/Users/Human Users
Adm_Apps/Users/Human Users/Internal
Adm_Apps/Users/Human Users/External
Adm_Apps/Users/Non-Human Users
Adm_Apps/Users/Non-Human Users/Internal
Adm_Apps/Users/Non-Human Users/External

比我使用的腳本:

$OUStructure = Get-Content "C:\Users\admr_mornevr\Documents\PowerShell\OdekSpace\Active Directory\OUs\OdekOUStructure.txt"

$CreateOUs = "`$MyDomain = Get-ADDomain | Select DistinguishedName;
`$connectionString = 'LDAP://' + `$MyDomain.DistinguishedName;
`$objDomain  = [ADSI]`$connectionString;

$OUStructure | Foreach-Object {
  `$domain = `$MyDomain.DistinguishedName
  `$newOU = ''
  `$ous = (Split-Path `$_ -Parent).Split('\')
  [array]::Reverse(`$ous)
  `$ous | Foreach-Object {
    if (`$_.Length -eq 0) {
      return
    }
    `$newOU = `$newOU + ',OU=' + `$_
  }
  `$nyOUNavn = Split-Path `$_ -Leaf
  `$newOU = 'OU=' + `$nyOUNavn + `$newOU

  `$objOU = `$objDomain.Create('OrganizationalUnit', `$newOU)
  `$objOU.SetInfo()
}"
Invoke-VMScript -ScriptType PowerShell -ScriptText $CreateOUs -VM $DCName -GuestCredentials $DomainCredential

我將上面的代碼修改如下:

$OUs = Import-Csv 'Path to CSV File'

$CreateOUs = "foreach ($OU in $OUs)
{
`$Name = `$(`$OU.Name)
`$Path = `$(`$OU.Path)
New-ADOrganizationalUnit -Name `$Name -Path `$Path
}"
Invoke-VMScript -ScriptType PowerShell -ScriptText $CreateOUs -VM $DCName -GuestCredentials $DomainCredential

因此,感謝@Theo 為我指明了正確的方向,感謝 LucD 對 VMware 社區的支持。 下面的代碼對我有用。

$OUs = Import-Csv 'C:\PathtoFile.csv' 

$CreateOUs = @'
New-ADOrganizationalUnit -Name $($Name) -Path $($Path)
'@

foreach ($OU in $OUs)
{
$Name = $($OU.Name)
$Path = $($OU.Path)
$ExecuteCmd = $ExecutionContext.InvokeCommand.ExpandString($CreateOUs) 
Invoke-VMScript -ScriptType Powershell -ScriptText $ExecuteCmd -VM $DCName -GuestCredential $DomainCredential
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM