Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 克隆Hyper-V虚拟机并将其添加到域_C#_Powershell_Clone_Hyper V_Windows Server 2016 - Fatal编程技术网

C# 克隆Hyper-V虚拟机并将其添加到域

C# 克隆Hyper-V虚拟机并将其添加到域,c#,powershell,clone,hyper-v,windows-server-2016,C#,Powershell,Clone,Hyper V,Windows Server 2016,我有一个独立的,不是群集的,而是安装了Hyper-V角色的Windows Server 2016域。 我需要每天克隆虚拟机,所以我使用C和PowerShell编写了一些小东西 VHD的并行拷贝 公共静态无效字符串[]args{ var sourceFile=args[0]; var distanationDir=args[1]; int numOfCopies=Int32.Parseargs[2]; //var sourceFile=$@C:\temp\123.VHD; //var distan

我有一个独立的,不是群集的,而是安装了Hyper-V角色的Windows Server 2016域。 我需要每天克隆虚拟机,所以我使用C和PowerShell编写了一些小东西

VHD的并行拷贝

公共静态无效字符串[]args{ var sourceFile=args[0]; var distanationDir=args[1]; int numOfCopies=Int32.Parseargs[2]; //var sourceFile=$@C:\temp\123.VHD; //var distanationDir=$@C:\temp\vhds\; //int numOfCopies=15; StartJobsourceFile、distanationDir、numOfCopies、random; } 私有静态void StartJobstring源文件,字符串distanationdir,int numfocopies,随机{ 整数计数=0; Parallel.For0,numOfCopies,i=>{ Console.WriteLinei={0},thread={1},i,thread.CurrentThread.ManagedThreadId; 试一试{ 字符串atrib=Path.GetExtensionsourcefile; 字符串guid=guid.NewGuid.toString; var copyNew=Path.getfilenamwithoutextensionsourcefile+guid+atrib; File.Copysourcefile,Path.CombineInstanceDir+'\\',copyNew; 安慰WriteLine@Started{copyNew}; }捕获IOException复制错误{ Console.WriteLinecopyError.Message; } } } 第二步是使用此PowerShell脚本创建VM,该脚本使用现有VHDX创建VM

$from=81 $howmany=30 $files=获取子项-路径E:\Hyper-v-Filter*.vhdx 如果$files{ 写入主机变量不为null 试一试{ $vmIndex=$from 对于$i=0;$i-lt$多少;$i++{ 写主机启动 新Vm-名称AUT-TA$vmIndex-路径E:\Hyper-v\虚拟机-内存启动字节4294967296-VHDPath$文件[$i]。全名-SwitchName Broadcom NetXtreme千兆以太网4-虚拟交换机 写入创建的主机Vm+AUT-TA$vmIndex $vmIndex++ 写入主机端 } 出口 }抓住{ [System.Exception]捕获到系统异常 } }否则{ 写主机没有这样的文件 出口 } 最后,我需要将它们加入到域中,但我需要一些脚本,这些脚本将为此主机上的每个VM运行,并将其添加到域中


因为您应该能够使用PowerShell Direct,所以您只需获取要在其上执行的VM,然后调用命令-VMName添加计算机命令。您的注释意味着您希望在脚本中保存密码。请记住这对安全的影响

$domain = "myDomain" 
$password = "myPassword!" | ConvertTo-SecureString -asPlainText -Force 
$username = "$domain\myUserAccount" 
$credential = New-Object System.Management.Automation.PSCredential($username,$passwor‌​d) 
Get-VM | ForEach-Object { Invoke-Command -VMName $_.name -ScriptBlock {Add-Computer -DomainName $using:domain -Credential $using:credentia}}
这是一个解决方案: 在克隆或复制VHD之前,在客户端和服务器上运行此命令

Enable-PSRemoting -Force
winrm set winrm/config/client '@{TrustedHosts="*"}'
从hyper-v主机运行后,此操作仅用于添加一个VM,但您可以为主机上VM的每个ip创建它

#Local Cred 
$passwordLoc = "Password" 
$usernameLoc = "localhost\Administrator"
$credentialsLoc = New-Object System.Management.Automation.PsCredential($usernameLoc, (ConvertTo-SecureString $passwordLoc -AsPlainText -Force))
$ses = New-PSSession 192.168.90.91 -Credential $credentialsLoc 
##Domain Cred
$domain = "domain.com"
$password = "Password"
$username = "$domain\PowerUser"
$credentials = New-Object System.Management.Automation.PsCredential($username, (ConvertTo-SecureString $password -AsPlainText -Force)) 
Invoke-command -Session $ses -ScriptBlock {
Add-Computer -DomainName $using:domain -Credential $using:credentials  -ErrorAction Stop 
Rename-Computer -NewName "AUT-TA41" -DomainCredential $using:credentials -Force -ErrorAction Stop
Restart-Computer -ErrorAction Stop}

这很有效

有些人这样认为$domain=myDomain$password=myPassword!|ConvertTo SecureString-asPlainText-Force$username=$domain\myUserAccount$credential=新对象系统.管理.自动化.PSCredential$username,$password添加计算机-DomainName$domain-credential$credential。但有人认为我可以在服务器hyper-v上运行,并将所有VM添加到domainSupply值中,以获得以下参数:发生了Windows PowerShell无法处理的错误。远程会话可能已结束。+CategoryInfo:OpenError:AUT-TA100:String[],PSRemotingDataStructureException+FullyQualifiedErrorId:PSSessionStateBreaked这是我收到的,每次需要输入时credentials@AndreyDzhezhora,您是否更改了$domain、$password和$username以反映您的域值?如果是这样,它应该会起作用@FoxDeploy是的,当然。也许我需要在虚拟机上做一些先决条件?运行一些命令?