4

I'm trying to automate the setup of a domain controller with a DHCP server in a Hyper-V environment. Things go wrong when I try to install the DHCP server. Actually when I try to import the DhcpServer powershell module.

This is the relevant code, where DC1 is the name of the vm:

$Cred = Get-Credential

[scriptblock]$Scriptblock = {
    Write-Host "Install DHCP feature & management tools"
    Install-WindowsFeature dhcp -IncludeManagementTools

    Write-Host "Import DhcpServer module"
    Import-Module DhcpServer # Error happens here
}

$Session = New-PSSession -VMName "DC1" -Credential $Cred
Invoke-Command -Session $Session -ScriptBlock $Scriptblock 

This will install the DHCP server role on the vm using powershell direct. However the Import-Module statement fails with this error:

Cannot find the Windows PowerShell data file 'DhcpServerMigration.psd1' in directory 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\DhcpServer\en\', or in any parent culture 
directories.
    + CategoryInfo          : ObjectNotFound: (C:\Windows\syst...rMigration.psd1:String) [Import-LocalizedData], PSInvalidOperationException
    + FullyQualifiedErrorId : ImportLocalizedData,Microsoft.PowerShell.Commands.ImportLocalizedData
    + PSComputerName        : DC1

I would like to note that the file does exist at C:\Windows\system32\WindowsPowerShell\v1.0\Modules\DhcpServer\en\DhcpServerMigration.psd1 Also when I try to import the module with the absolute path, I get the same error.

When I perform the steps manually by executing the steps below:

$Cred = Get-Credential
$Session = New-PSSession -VMName "DC1" -Credential $Cred
Enter-PSSession $Session
Install-WindowsFeature dhcp -IncludeManagementTools
Import-Module DhcpServer

Everything works, this is a transcript:

PS C:\_repo\> $Session = New-PSSession -VMName "DC1" -Credential $Cred
PS C:\_repo\> Enter-PSSession $session
[DC1]: PS C:\Users\administrator\Documents> Install-WindowsFeature dhcp -IncludeManagementTools

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {DHCP Server, Remote Server Administration...

[DC1]: PS C:\Users\administrator\Documents> Import-Module DhcpServer

What am I missing? Why does the first option - with invoke-command fail? Both options use powershell direct.

Update after suggestions of @FSCKur:

The original setup was a host with a en-US culture and a vm with a nl-BE culture. As demonstrated the Import-Module fails.

I created a new vm with a en-US culture. Now Import-Module works as expected.

An updated question is then why does the Import-Module fail when the culture is different between the host and the vm when used with the Invoke-Command cmdlet?

Chumpy
  • 43
  • 1
  • 7
  • 1
    Can you run `Get-Culture` on your box, interactively in the PSSession, and with `Invoke-Command`, and let us know if there is any difference? – FSCKur Mar 17 '19 at 17:45
  • Thank you for the prompt response. When i run `Get-Culture` On the box - host: `en-US` With `Enter-PSSession` in the vm: `nl-BE` With `Invoke-Command` in the vm: `nl-BE` In the next few days I will try to get a vm with a `en-US` culture and see how that behaves. – Chumpy Mar 17 '19 at 18:42

1 Answers1

0
$pso = New-PSSessionOption -Culture "en-US" -UICulture "en-US"
$VM = New-PSSession -ComputerName $vm -Credential $cred -Name VM -SessionOption $pso
ChristianYami
  • 586
  • 1
  • 9
  • 17
Dvd Wang
  • 1
  • 1
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jan 16 '21 at 11:05