首页 > 解决方案 > WinRM 超时 Packer,可能是端口问题?使用 MWrock 模板

问题描述

像很多人一样,我在让 winRM 工作时遇到了麻烦。我正在使用 MWrock 模板,所以理论上它应该已经用我正在使用的他的脚本修复了,但不幸的是它只是超时了......

这是我的 json 文件:

{
  "builders": [
    {
      "type": "virtualbox-iso",
      "vboxmanage": [
        [ "modifyvm", "{{.Name}}", "--memory", "2048" ],
        [ "modifyvm", "{{.Name}}", "--vram", "48" ],
        [ "modifyvm", "{{.Name}}", "--cpus", "2" ]
      ],
      "guest_additions_mode": "attach",
      "guest_os_type": "Windows2016_64",
      "headless": "true",
      "iso_url": "iso/Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh.iso",
      "iso_checksum": "70721288bbcdfe3239d8f8c0fae55f1f",
      "iso_checksum_type": "md5",
      "communicator": "winrm",
      "winrm_username": "Project",
      "winrm_password": "Project",
      "winrm_timeout": "12h",
      "shutdown_command": "C:/Windows/Panther/Unattend/packer_shutdown.bat",
      "shutdown_timeout": "15m",
      "floppy_files": [
        "answer_files/Autounattend.xml",
        "scripts/winrm.ps1"
      ]
    }
  ],
  "provisioners": [
    {
      "type": "powershell",
      "script": "scripts/windows-updates.ps1",
      "elevated_user": "Project",
      "elevated_password": "Project"
    },
    {
      "type": "windows-restart",
      "restart_timeout": "15m"
    },
    {
      "type": "powershell",
      "execute_command": "powershell -ExecutionPolicy Bypass \"& { {{.Vars}}{{.Path}}; exit $LastExitCode}\"",
      "scripts": [
          "enable-rdp.ps1",
          "guest-tools.ps1"
      ]
  },
    {
      "type": "powershell",
      "script": "scripts/after-reboot.ps1",
      "elevated_user": "Project",
      "elevated_password": "Project"
    },
    {
      "type": "powershell",
      "script": "scripts/cleanup.ps1",
      "elevated_user": "Project",
      "elevated_password": "Project"
    }
  ]
}

我的winrm.ps1:

netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in localport=5985 protocol=TCP action=allow
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'

这个 winrm.ps1 包含在我的自动无人参与文件中,第 137 行:`

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="windowsPE">
    <!-- look for drivers on floppy -->
    <component name="Microsoft-Windows-PnpCustomizationsWinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <DriverPaths>
            <PathAndCredentials wcm:keyValue="1" wcm:action="add">
                <Path>A:\</Path>
            </PathAndCredentials>
        </DriverPaths>
    </component>

        <component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
            <SetupUILanguage>
                <UILanguage>en-US</UILanguage>
            </SetupUILanguage>
            <InputLocale>en-US</InputLocale>
            <SystemLocale>en-US</SystemLocale>
            <UILanguage>en-US</UILanguage>
            <UILanguageFallback>en-US</UILanguageFallback>
            <UserLocale>en-US</UserLocale>
        </component>
        <component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
            <DiskConfiguration>
                <Disk wcm:action="add">
                    <CreatePartitions>
                        <CreatePartition wcm:action="add">
                            <Type>Primary</Type>
                            <Order>1</Order>
                            <Size>350</Size>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>2</Order>
                            <Type>Primary</Type>
                            <Extend>true</Extend>
                        </CreatePartition>
                    </CreatePartitions>
                    <ModifyPartitions>
                        <ModifyPartition wcm:action="add">
                            <Active>true</Active>
                            <Format>NTFS</Format>
                            <Label>boot</Label>
                            <Order>1</Order>
                            <PartitionID>1</PartitionID>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <Format>NTFS</Format>
                            <Label>Windows 2016</Label>
                            <Letter>C</Letter>
                            <Order>2</Order>
                            <PartitionID>2</PartitionID>
                        </ModifyPartition>
                    </ModifyPartitions>
                    <DiskID>0</DiskID>
                    <WillWipeDisk>true</WillWipeDisk>
                </Disk>
            </DiskConfiguration>
            <ImageInstall>
                <OSImage>
                    <InstallFrom>
                        <MetaData wcm:action="add">
                            <Key>/IMAGE/NAME </Key>
                            <Value>Windows Server 2016 SERVERDATACENTER</Value>
                        </MetaData>
                    </InstallFrom>
                    <InstallTo>
                        <DiskID>0</DiskID>
                        <PartitionID>2</PartitionID>
                    </InstallTo>
                </OSImage>
            </ImageInstall>
            <UserData>
                <ProductKey>
                    <WillShowUI>OnError</WillShowUI>
                </ProductKey>
                <AcceptEula>true</AcceptEula>
                <FullName>EvansPackerproject</FullName>
                <Organization>Evan</Organization>
            </UserData>
        </component>
    </settings>  
  <settings pass="generalize">
    <component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SkipRearm>1</SkipRearm>
    </component>
    <component name="Microsoft-Windows-PnpSysprep" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <PersistAllDeviceInstalls>false</PersistAllDeviceInstalls>
      <DoNotCleanUpNonPresentDevices>false</DoNotCleanUpNonPresentDevices>
    </component>
  </settings>
  <settings pass="oobeSystem">
    <component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <InputLocale>en-US</InputLocale>
      <SystemLocale>en-US</SystemLocale>
      <UILanguage>en-US</UILanguage>
      <UserLocale>en-US</UserLocale>
    </component>
    <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <OOBE>
                <HideEULAPage>true</HideEULAPage>
                <HideLocalAccountScreen>true</HideLocalAccountScreen>
                <HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
                <HideOnlineAccountScreens>true</HideOnlineAccountScreens>
                <HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
                <NetworkLocation>Home</NetworkLocation>
                <ProtectYourPC>1</ProtectYourPC>
            </OOBE>
      <TimeZone>UTC</TimeZone>
      <UserAccounts>
        <AdministratorPassword>
          <Value>vagrant</Value>
          <PlainText>true</PlainText>
        </AdministratorPassword>
        <LocalAccounts>
          <LocalAccount wcm:action="add">
            <Password>
              <Value>vagrant</Value>
              <PlainText>true</PlainText>
            </Password>
            <Group>administrators</Group>
            <DisplayName>Vagrant</DisplayName>
            <Name>vagrant</Name>
            <Description>Vagrant User</Description>
          </LocalAccount>
        </LocalAccounts>
      </UserAccounts>
            <AutoLogon>
                <Password>
                    <Value>vagrant</Value>
                    <PlainText>true</PlainText>
                </Password>
                <Enabled>true</Enabled>
                <Username>vagrant</Username>
            </AutoLogon>
      <FirstLogonCommands>
        <SynchronousCommand wcm:action="add">
           <CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\winrm.ps1</CommandLine>
           <Order>1</Order>
        </SynchronousCommand>
      </FirstLogonCommands>
    </component>
  </settings>
    <settings pass="specialize">
        <component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-ServerManager-SvrMgrNc" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
            <DoNotOpenServerManagerAtLogon>true</DoNotOpenServerManagerAtLogon>
        </component>
        <component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-IE-ESC" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
            <IEHardenAdmin>false</IEHardenAdmin>
            <IEHardenUser>false</IEHardenUser>
        </component>
        <component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-OutOfBoxExperience" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
            <DoNotOpenInitialConfigurationTasksAtLogon>true</DoNotOpenInitialConfigurationTasksAtLogon>
        </component>
    </settings>
</unattend>`

当它通过构建运行时,我注意到它没有使用正确的端口......

==> virtualbox-iso: Pausing after run of step 'StepAttachFloppy'. Press enter to continue. ==> virtualbox-iso: Creating forwarded port mapping for communicator (SSH, WinRM, etc) (host port 3664)
==> virtualbox-iso: Pausing after run of step 'StepForwardSSH'. Press enter to continue. ==> virtualbox-iso: Executing custom VBoxManage commands...
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso-1574451132 --memory 2048
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso-1574451132 --vram 48
    virtualbox-iso: Executing: modifyvm packer-virtualbox-iso-1574451132 --cpus 2

==> virtualbox-iso: Pausing after run of step 'StepVBoxManage'. Press enter to continue. ==> virtualbox-iso: Starting the virtual machine...

==> virtualbox-iso: Pausing after run of step 'StepRun'. Press enter to continue. ==> virtualbox-iso: Waiting 10s for boot...
==> virtualbox-iso: Typing the boot command...
==> virtualbox-iso: Pausing after run of step 'boot_command: '. Press enter to continue. ==> virtualbox-iso: Pausing after run of step 'StepTypeBootCommand'. Press enter to continue. ==> virtualbox-iso: Using winrm communicator to connect: 127.0.0.1
==> virtualbox-iso: Waiting for WinRM to become available...

我看过其他关于港口的帖子,但没有明确的解决方案。

任何帮助或正确方向的观点将不胜感激。

标签: jsonxmlpowershellpackerwinrm

解决方案


通过在我的本地计算机上正确设置 WINRM 解决...

按照这些故障排除步骤,然后将 json 文件更改为不包括端口转发,因为 VirtualBox 不需要它。

它说它在cmd运行时使用的端口packer只是它转发到winrm来宾端口的主机。

{
  "builders": [
    {
      "type": "virtualbox-iso",
      "vm_name": "Evans project",
      "vboxmanage": [
        [ "modifyvm", "{{.Name}}", "--memory", "2048" ],
        [ "modifyvm", "{{.Name}}", "--vram", "48" ],
        [ "modifyvm", "{{.Name}}", "--cpus", "2" ]
      ],
      "guest_additions_mode": "attach",
      "guest_os_type": "Windows2016_64",
      "headless": "false",
      "iso_url": "iso/Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh.iso",
      "iso_checksum": "70721288bbcdfe3239d8f8c0fae55f1f",
      "iso_checksum_type": "md5",
      "communicator": "winrm",
      "winrm_host": "5985",
      "winrm_username": "Project",
      "winrm_password": "Project",
      "winrm_timeout": "12h",
      "shutdown_command": "C:/Windows/Panther/Unattend/packer_shutdown.bat",
      "shutdown_timeout": "15m",
      "floppy_files": [
        "answer_files/Autounattend.xml",
        "scripts/winrmv2.ps1"
      ]
    }
  ],
  "provisioners": [
    {
      "type": "powershell",
      "scripts": "scripts/windows-updates.ps1",
      "elevated_user": "Project",
      "elevated_password": "Project"
    },
    {
      "type": "windows-restart",
      "restart_timeout": "15m"
    },
    {
      "type": "powershell",
      "execute_command": "powershell -ExecutionPolicy Bypass \"& { {{.Vars}}{{.Path}}; exit $LastExitCode}\"",
      "scripts": [
          "enable-rdp.ps1",
          "guest-tools.ps1"
      ]
    },
    {
      "type": "powershell",
      "script": "scripts/after-reboot.ps1",
      "elevated_user": "Project",
      "elevated_password": "Project"
    },
    {
      "type": "powershell",
      "script": "scripts/cleanup.ps1",
      "elevated_user": "Project",
      "elevated_password": "Project"
    }
  ]
}

推荐阅读