onpremcloudguy / PSNewCMENV

PowerShell script to create SCCM environment.
12 stars 5 forks source link

Convert-WindowsImage module using `-PassThru` with `Dismount-DiskImage` #3

Open codaamok opened 4 years ago

codaamok commented 4 years ago

Hey, this looks awesome and I can't wait to start using it.

The Convert-WindowsImage module uses Dismount-DiskImage with the -PassThru parameter. The Dismount-DiskImage cmdlet has changed behaviour in 1809 onwards where the -PassThru parameter doesn't exist. Microsoft docs are yet to be updated on this, I've left feedback.

As a result a lot goes wrong. I know it's out of your hands but I'm just sharing this as an FYI. The Convert-WindowsImage module hasn't been updated for a while, I'm not sure if it will get fixed. Regardless I created an issue in the repo where the module lives and on the doc page for the Dismount-DiskImage function.

Maybe for your own interest, the below is what's printed to console in this scenario (note I also have a Pester issue where -Show doesn't exist, but I'll check that out tomorrow..):

[acc@DESKTOP-PFFNJ90] [20:44:01] PS C:\VMs\PSNewCMENV
> .\LabTest.ps1
Invoke-Pester : A parameter cannot be found that matches parameter name 'Show'.
At C:\VMs\PSNewCMENV\NewENV.ps1:36 char:68
+ ... HDX = Invoke-Pester -TestName "Reference-VHDX" -PassThru -Show Passed
+                                                              ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Pester], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-Pester

NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The                                                                                                                               NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\acc\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running                                                                                                                           'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and                                                                                                                                import the NuGet provider now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from                                                                                                                              'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): a
Dismount-DiskImage : A parameter cannot be found that matches parameter name 'PassThru'.
At C:\Program Files\WindowsPowerShell\Modules\Convert-Windowsimage\10.0\Convert-WindowsImage.psm1:2401 char:77
+ ... $DismountDiskImage = Dismount-DiskImage -ImagePath $IsoPath -PassThru
+                                                                 ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Dismount-DiskImage], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Dismount-DiskImage

Invoke-Pester : A parameter cannot be found that matches parameter name 'Show'.
At C:\VMs\PSNewCMENV\NewENV.ps1:51 char:61
+ ...    $TNetwork = Invoke-Pester -TestName "vSwitch" -PassThru -Show None
+                                                                ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Pester], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-Pester

Invoke-Pester : A parameter cannot be found that matches parameter name 'show'.
At C:\VMs\PSNewCMENV\newRRASServer.ps1:48 char:52
+     if (((Invoke-Pester -TestName "RRAS" -PassThru -show None).TestRe ...
+                                                    ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Pester], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-Pester

Get-VMNetworkAdapter : Hyper-V was unable to find a virtual machine with name "RRAS".
At C:\VMs\PSNewCMENV\newRRASServer.ps1:110 char:10
+     if ((Get-VMNetworkAdapter -VMName $RRASConfig.name | Where-Object ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VMNetworkAdapter], VirtualizationException
    + FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.GetVMNetworkAdapter

Invoke-Pester : A parameter cannot be found that matches parameter name 'show'.
At C:\VMs\PSNewCMENV\newRRASServer.ps1:111 char:56
+         if (((Invoke-Pester -TestName "RRAS" -PassThru -show None).Te ...
+                                                        ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Pester], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-Pester

Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At C:\VMs\PSNewCMENV\newRRASServer.ps1:132 char:33
+         Invoke-Command -Session $RRASConfigSession -ScriptBlock { Set ...
+                                 ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand                                                                                                                               
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument                                                                                                                            that is not null or empty, and then try the command again.
At C:\VMs\PSNewCMENV\newRRASServer.ps1:134 char:33
+         Invoke-Command -Session $RRASConfigSession -ScriptBlock { Set ...
+                                 ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand                                                                                                                               
Remove-PSSession : Cannot bind argument to parameter 'Session' because it is null.
At C:\VMs\PSNewCMENV\newRRASServer.ps1:135 char:30
+         $RRASConfigSession | Remove-PSSession
+                              ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-PSSession], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemovePSSe                                                                                                                              ssionCommand

Describing RRAS
 [-] RRAS VHDX Should exist 576ms
   Expected: {True}
   But was:  {False}
   38:     it 'RRAS VHDX Should exist' { $tRRASVHDXExists | should be $true }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 38
 [-] RRAS Server Should exist 133ms
   Expected: {1}
   But was:  {0}
   39:     it 'RRAS Server Should exist' { $tRRASExist | should be 1 }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 39
 [-] RRAS Server is Running 20ms
   Expected: {1}
   But was:  {0}
   40:     it 'RRAS Server is Running' { $trrasrunning | should be 1 }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 40
 [!] RRAS Routing Installed 7ms
 [!] RRAS External NIC Renamed 3ms
 [!] RRAS Lab NIC Renamed 2ms
 [!] RRAS Lab IP Address Set 1ms
 [!] RRAS VPN enabled 1ms
 [!] RRAS has access to Internet 1ms
Tests completed in 749ms
Passed: 0 Failed: 3 Skipped: 6 Pending: 0 Inconclusive: 0
Invoke-Pester : A parameter cannot be found that matches parameter name 'show'.
At C:\VMs\PSNewCMENV\newDCServer.ps1:63 char:51
+     if (!((Invoke-Pester -TestName "DC" -PassThru -show None).TestRes ...
+                                                   ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Pester], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-Pester

Describing DC
 [-] DC VHDX Should Exist 75ms
   Expected: {True}
   But was:  {False}
   68:     it 'DC VHDX Should Exist' { $TDCVHDXExists | should be $true }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 68
 [-] DC Should Exist 18ms
   Expected: {1}
   But was:  {0}
   69:     it "DC Should Exist" { $TDCExists | should be 1 }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 69
 [-] DC Should be running 21ms
   Expected: {1}
   But was:  {0}
   70:     it "DC Should be running" { $TDCRunning | should be 1 }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 70
 [!] DC IP Address 8ms
 [!] DC has access to Internet 1ms
 [!] DC Domain Services Installed 1ms
 [!] DC Promoted 2ms
 [!] DC DHCP Scope Active 2ms
 [!] DC SCCM Servers Group 2ms
Tests completed in 135ms
Passed: 0 Failed: 3 Skipped: 6 Pending: 0 Inconclusive: 0
Invoke-Pester : A parameter cannot be found that matches parameter name 'show'.
At C:\VMs\PSNewCMENV\NewCAServer.PS1:63 char:50
+     if (((Invoke-Pester -TestName "CA" -PassThru -show None).TestResu ...
+                                                  ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Pester], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-Pester

Invoke-Pester : A parameter cannot be found that matches parameter name 'show'.
At C:\VMs\PSNewCMENV\NewSCCMServer.PS1:122 char:63
+ ...  (((Invoke-Pester -tag $cmConfig.CMServerType -PassThru -show None).T ...
+                                                             ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Pester], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-Pester

Describing CM
 [-] CM VHDX Should Exist 120ms
   Expected: {True}
   But was:  {False}
   192:         it 'CM VHDX Should Exist' { $TCMVHDXExists | should be $true }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 192
 [-] CM Should Exist 18ms
   Expected: {1}
   But was:  {0}
   193:         it 'CM Should Exist' { $TCMExists | should be 1 }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 193
 [-] CM Should be running 20ms
   But was:  {0}
   194:         it 'CM Should be running' { $TCMRunning | should be 1 }
   at <ScriptBlock>, C:\VMs\PSNewCMENV\NewENV.Tests.ps1: line 194
 [!] CM IP Address 7ms
 [!] CM has access to Internet 1ms
 [!] CM has access to MMS.lab 1ms
 [!] CM .Net Feature installed 1ms
 [!] CM Features are installed 2ms
 [!] CM SQL Instance is installed 4ms
 [!] CM ADK Installed 2ms
 [!] CM Server in Group 1ms
 [!] CM SCCM Installed 1ms
 [!] CM SCCM Console Installed 1ms
 [!] CM Site Boundary added 1ms
 [!] CM System Discovery enabled 1ms
Tests completed in 189ms
Passed: 0 Failed: 3 Skipped: 12 Pending: 0 Inconclusive: 0
CM Server Completed: 11/28/2019 20:50:04
codaamok commented 4 years ago

So, this turned out to be really easy to fix.

For the Dismount-DiskImage and -PassThru thing, just search and replace Dismount-DiskImage and remove -PassThru in the Convert-WindowsImage module (only 3 lines):

Get-Module Convert-WindowsImage -ListAvailable | Select-Object -ExpandProperty Path

As for Pester, if you're like me and on 1809 or newer, following the Pester docs, just rip out the module and reinstall to grab the latest:

$module = "C:\Program Files\WindowsPowerShell\Modules\Pester"
takeown /F $module /A /R
icacls $module /reset
icacls $module /grant Administrators:'F' /inheritance:d /T
Remove-Item -Path $Module -Recurse -Force -Confirm:$false

Install-Module -Name Pester -Force
AdamGrossTX commented 4 years ago

The issue is in the source of the Convert-WindowsImage module. I've submitted a PR for it here:

https://github.com/MicrosoftDocs/Virtualization-Documentation/pull/1260

Doesn't look like anyone cares - but you can pull my version of the module if that helps.