Hi,
I am having trouble executing this command from a batch file on few of the VMs and I am getting below error.
Please help
Command
Powershell.exe -noprofile -executionpolicy bypass -command "&{start-process powershell -ArgumentList -noprofile 'Get-DnsClientServerAddress | where {$_.ServerAddresses -contains 192.168.15.5} | Set-DnsClientServerAddress -ServerAddresses 192.168.5.100,192.168.5.101' -Verbose -Confirm:$false; ipconfig /flushdns; ipconfig /flushdns; Clear-DnsClientCache; Register-DnsClient -verb RunAs}"
Error
Start-Process : A positional parameter cannot be found that accepts argument 'Get-DnsClientServerAddress | where
{$_.ServerAddresses -contains 192.168.15.5} | Set-DnsClientServerAddress -ServerAddresses 192.168.5.100,192.168.5.101'.
At line:1 char:3
+ &{start-process powershell -ArgumentList -noprofile 'Get-DnsClientSer ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
The ArgumentList parameter expects an array with arguments.
I'm not sure what you are trying to do, but the following should get rid of the error.
$sProcess = @{
FilePath = 'powershell.exe'
ArgumentList = '-noprofile',
'-verb runas',
'-command "&{Get-DnsClientServerAddress | where {$_.ServerAddresses -contains 192.168.15.5} | Set-DnsClientServerAddress -ServerAddresses 192.168.5.100,192.168.5.101 -Verbose -Confirm:$false; ipconfig /flushdns; ipconfig /flushdns; Clear-DnsClientCache; Register-DnsClient}"'
}
Start-Process @sProcess
'@
powershell.exe -noprofile -executionpolicy bypass -command "&{$code}"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I am trying to run script as .cmd file for DR Servers to update the DNS while performing the DR.
Below is the script, which I am using.
Currently I am getting the below error
C:\DR>Powershell.exe -noprofile -executionpolicy bypass "&{start-process powershell -ArgumentList '-noprofile', '-verb r
unas', '-command " & {Get-DnsClientServerAddress | where {$_.ServerAddresses -contains 192.168.15.5} | Set-DnsClien
tServerAddress -ServerAddresses 192.168.5.100,192.168.5.101 -Verbose -Confirm:$false; ipconfig /flushdns; ipconfig /flus
hdns; Clear-DnsClientCache; Register-DnsClient}"' }"
The string is missing the terminator: '.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
'{Get-DnsClientServerAddress' is not recognized as an internal or external command,
operable program or batch file.
Did you try it like this?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes LucD.
I am getting the below error
The string is missing the terminator: '.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
'{Get-DnsClientServerAddress' is not recognized as an internal or external command,
operable program or batch file.
It's a quoting issue.
The following seems to work for me
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Now, I am not getting any error but DNS IP is not getting changed.
If I execute the below command directly, DNS is getting changed. but when added with script, DNS is not getting change.
Get-DnsClientServerAddress | where {$_.ServerAddresses -contains 192.168.15.5} | Set-DnsClientServerAddress -ServerAddresses 192.168.5.100,192.168.5.101 -Verbose -Confirm:$false
You probably need elevation for the cmdlet to work.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes LucD, I tried as below also, still no luck
powershell.exe -noprofile -executionpolicy bypass -command "&{Start-Process -FilePath powershell.exe -ArgumentList '-noprofile','-command &{Get-DnsClientServerAddress | where {$_.ServerAddresses -contains 172.24.255.5} | Set-DnsClientServerAddress -ServerAddresses 172.25.255.5,172.25.255.6 -Verbose -Confirm:$false; ipconfig /flushdns; ipconfig /flushdns; Clear-DnsClientCache; Register-DnsClient -verb RunAs}'}"
all the other commands work, but first command for changing the DNS still dont work.
Your initial call to powershell.exe will not run elevated.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference