ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [INFO] VMware PowerCLI - vSphere/Horizon/Windows에서 활용하기
    Virtualization Infra/Server 2023. 12. 27. 19:42

    기본설정

    1) 원격 실행 정책 허용, 인증서 / CEIP 무시

    Set-executionpolicy remotesigend
    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
    Set-PowerCLIConfiguration -Scope User -ParticipateInCeip $false

     

    2) 텍스트 파일 가져오기

    $List = Get-Content -Path "<파일 경로 ex,D:\~>"

     

    3) csv 파일 데이터 가져와서 속성/데이터 분리하여 사용

    csv 파일 내용

    $List = Import-csv "<파일 경로 ex,D:\~>"

    foreach( $col in $List){
      $vm = $col.VMName
      $usr= $col.GuestVMUser
      $pwd=$col.GuestVMPassword }

     

    4) Progress-Bar

    전체 작업 중 현재 작업 내용 및 진행 퍼센트를 progress-bar를 통해 GUI로 확인 가능

    Write-Progress -Activity '<Activity_Name>' -CurrentOperation $col -PercentComplete(($counter/$List.count)*100) 

     

    5) Sleep

    20초 대기 후 다음 작업 시작

     Start-Sleep -Seconds 20

     

    6) 파일 복사

    Copy-Item -Path "<복사할 파일 경로 ex,D:\test01.csv>" -Destination "<붙여넣을 파일 위치 ex,D:\test\>"

     

    7) 배열 생성

    $cssvr = $varcs1,$varcs2,$varcs3 | %{$_}

     

    8) vCenter/Horizon Server 연결

    #변수 선언
    $vc_ip="<IP>"
    $vc_admin="<login_id>"
    $vc_pwd="<password>"

    $cs_ip="<IP>"
    $cs_admin="<login_id>"
    $cs_pwd="<password>"


    #vCenter 연결
    Connect-VIServer -Server $vc_ip -User $vc_admin -Password $vc_pwd -Port 443

    #Horizon 연결

    Connect-HVServer -Server $cs_ip -User $cs_admin -Password $cs_pwd

     

     

    vSphere

    VM 네트워크 어댑터 변경

    1) 네트워크 어댑터 변경

    $net_new="<Network Adapter Name>"

    Get-VM -Name "<VM_Name>" | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $net_new -Confirm:$false -RunAsync

     

    사용자지정규격에 네트워크 정보 지정(임시)

    - 기존에 생성된 '사용자지정규격'에 네트워크 정보를 재설정

    $usercustomspec : 사용자 지정 규격

    IpMode UseStaticIP : 고정 IP 모드 (dhcp의 경우 UseDhcp 사용)

    Get-OSCustomizationNicMapping $usercustomspec | Set-OSCustomizationNicMapping -IpMode UseStaticIP $ip -SubnetMask $subnet -DefaultGateway $gw -dns $dns1,$dns2

     

    vCenter에 VM 배포

    - 기존에 생성된 '사용자지정규격'에 네트워크 정보를 재설정 해준 값으로 VM 배포

    $vmname : vm 이름, $template : 템플릿 명, $host : 배포 할 호스트, $nic : 네트워크 인터페이스

    $usercustomspec 사용자 지정 규격, $folder_location : 배포할 폴더 위치

    New-VM -Name $vmname -Template $template -VMHost $host -NetworkName $nic -OSCustomizationSpec $usercustomspec -Datastore $datastore -Location $folder_location

     

     

    Horizon

    VM 상태 확인

    $RebootVM=Get-HVMachineSummary -State AGENT_UNRECHABLE | Select -ExpandProperty Base | select Name | %{$_.Name}

     

    Horizon Agent Silent Installation

    1) MS Windows Installer 명령 옵션

    Horizon 설치 관리자는 MSI(Microsoft Windows Installer) 표준으로 구성되어 있기 때문에 구성요소의 자동 설치를 하려면  MSI 명령줄 옵션을 사용해야 한다. 

    /s : 대화식 상자를 디스플레이 하지 않음
    /v"<옵션>" : MSI 설치 속성값을 넣어 설치 제어를 진행

     

    2) Horizon Agent의 원하는 옵션을 선택해서 Silent 설치 진행

    [~path]\VMware-Horizon-Agent-x86-y.y.y-xxxxxx.exe /s /v "/qn VDM_VC_MANAGED_AGENT=1 VDM_FLASH_URL_REDIRECTION=1 ADDLOCAL=Core,SVIAgent,ThinPrint,USB,FlashURLRedirection,RTAV"

     

    3) Horizon Agent 삭제

    - REBOOT=ReallySuppress : 시스템 재부팅 전 구성 작업 완료.

    - SUPPRESS_RUNONCE_CHECK=1 : 설치 및 재부팅 시 보류중인 윈도우 업데이트 무시

    [~path]\VMware-Horizon-Agent-x86-y.y.y-xxxxxx.exe /s /v "/qn REMOVE=ALL REBOOT=ReallySuppress SUPPRESS_RUNONCE_CHECK=1"

     

    Reference

    Horizon Agent Silent Installation Properties

    Horizon Agent Custom Setup Options

    MSI Cmdlet for Horizon Installation or Upgrade

     

    Horizon 데스크탑 풀/사용자 할당

    1) 데스크탑 풀 할당

    # Horizon PS Module
    Add-HVDesktop -PoolName $pool -Machine $vm

     

    2) 사용자 할당

    # Horizon PS Module
    Get-HVMachine -MachineName $vm | Set-HVMachine -User $usr

    # 연결서버 vdmadmin
    vdmadmin -L -d <desktop_pool_name> -m <vm_name> -u <domain\user_name>

     

    3) 사용자 할당 해제

    # 연결서버 vdmadmin
    vdmadmin -L -d <desktop_pool_name> -u <domain\user_name> -r

     

     

    Windows

    문장 출력 / 변수에 문장 저장

    1) 원하는 문장 출력

     - 출력할 변수와 문장을 나열해서 출력. 단어 혹은 문장은 쌍따옴표로 표시

    Write-Host $col "<Word or Sentense>"

     

    2) 원하는 명령어를 변수에 저장

    - 파일 복사 명령을 변수에 저장하는 예시 (홑따옴표 안에 원하는 명령어 작성)

    $script = 'Copy-Item -Path "<복사할 파일 경로 ex,D:\test01.csv>" -Destination "<붙여넣을 파일 위치 ex,D:\test\>"'

     

    - Network IPv4 설정값을 변수에 저장하는 예시

    $script = "netsh interface ipv4 address name=Eternet0 static $ip $subnet $gw"

     

    VM 내부에서 스크립트 실행

    - ScriptText : VM 내부에서 실행할 명령어 작성

    - ScriptType : 실행 언어 타입 (ex, PowerShell, Batch)

    - RunAsync : 병렬 실행 처리

    Invoke-VMScript -VM $vm -ScriptText $text -GuestUser $usr -GuestPassword $pwd -ScriptType Powershell -RunAsync

     

    설치된 앱 패키지 삭제

    1) 설치된 앱 패키지 확인

     - 삭제 가능하며, 종속성 없는 패키지만 전체 이름으로 확인 

    Get-AppxPackage -AllUser | Where-object {$_.IsFramework -Match 'False' -and $_.NonRemovable -Match 'False'} | Select-object PackageFullName

     

    2) 설치된 앱 패키지 삭제

     - 따옴표 없이 전체 이름 작성

    Remove-AppxPackage -AllUser <PackageFullName>

     

     

    Horizon Agent Silent Re-Installation (ALL)

    Set-executionpolicy remotesigend
    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
    Set-PowerCLIConfiguration -Scope User -ParticipateInCeip $false
    
    
    #변수 선언
    $vc_ip="<IP>"
    $vc_admin="<login_id>"
    $vc_pwd="<password>"
    
    $cs_ip="<IP>"
    $cs_admin="<login_id>"
    $cs_pwd="<password>"
    
    
    #CSV 작업목록 가져오기
    $List = Import-csv "<파일 경로>"
    $counter=0
    
    
    #변수에 명령 저장
    $Script1 = 'Copy-Item -Path "<복사할 파일 경로>" -Destination "<붙여넣을 파일 위치>"'
    $Script2 = 'c:\~.exe /s /v "/qn REMOVE=ALL REBOOT=ReallySuppress SUPPRESS_RUNONCE_CHECK=1"'
    $Script3 = 'c:\~.exe /s /v"qn LOCALADD=Core,USB"'
    
    
    #vCenter 연결
    Connect-VIServer -Server $vc_ip -User $vc_admin -Password $vc_pwd -Port 443
    
    #Horizon 연결
    Connect-HVServer -Server $cs_ip -User $cs_admin -Password $cs_pwd
    
    
    foreach($col in $List){
      $counter++
      $vm = $col.VMName
      $usr= $col.GuestVMUser
      $pwd=$col.GuestVMPassword
      
      #작업진행확인
      Write-Progress -Activity 'Processing' -CurrentOperation $vm -PercentComplete(($counter/$List.count)*100) 
      
      #GuestVM에 파일복사 후 20초 대기
      Invoke-VMScript -VM $vm -ScriptText $Script1 -GuestUser $usr -GuestPassword $pwd -ScriptType Powershell -RunAsync
      Start-Sleep -Seconds 20
      
      #GuestVM에 Horizon Agent Silent 삭제 후 20초 대기
      Invoke-VMScript -VM $vm -ScriptText $Script2 -GuestUser $usr -GuestPassword $pwd -ScriptType Powershell -RunAsync
      Start-Sleep -Seconds 20
      
      #GuestVM에 Horizon Agent Silent 설치 후 20초 대기
      Invoke-VMScript -VM $vm -ScriptText $Script3 -GuestUser $usr -GuestPassword $pwd -ScriptType Powershell -RunAsync
      Start-Sleep -Seconds 20
    }

     

     

Designed by Tistory.