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?