How can a powershell script can be invoked using chef?

I have a powershell script that intended to set dns server on client. How can I execute that script on client?

HELP!!!

Hi @waseemahammed

Use powershell_script resource inside your recipe.

This will solve your purpose.

Exampls from the same URL:

powershell_script 'write-to-interpolated-path' do
  code <<-EOH
    $stream = [System.IO.StreamWriter] "#{Chef::Config[:file_cache_path]}/powershell-test.txt"
    $stream.WriteLine("In #{Chef::Config[:file_cache_path]}...word.")
    $stream.close()
  EOH
end

Thanks for the info, @manishmehra

I'm not a windows guy but got a requirement and need to be working on powershell scripts.

I want to run this command on windows client-

Set-DNSClientServerAddress –interfaceIndex 12 –ServerAddresses ("10.10.10.10","8.8.8.8")

How can I integrate this in powershell_script block? Could you please help?

powershell_script 'set-dns-server-on-client' do
code <<-EOH
Set-DNSClientServerAddress –interfaceIndex 12 –ServerAddresses ("10.10.10.10","8.8.8.8")
EOH
end

Tries it already. Getting this error -
STDERR: C:\Users\Administrator\AppData\Local\Temp\2\chef-script20160922-6948-146gtcn.ps1 : A positional parameter cannot be
found that accepts argument 'System.Object'.
+ CategoryInfo : NotSpecified: (:slight_smile: [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,chef-script20160922-6948-146gtcn.ps1
---- End output of "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -InputFormat None -File "C:/Users/ADMINI~1/AppData/Local/Temp/2/chef-script20160922-6948-146gtcn.ps1" ----

I just ran that exact resource on a windows node successfully. What version of windows and powershell are you installing on?

Windows Server 2012 R2 and powershell 1.0

I’ve been using the Set-DNSClientServerAddress on Windows Server 2012 R2 with PowerShell version 4.0. Perhaps try updating your PowerShell version and try again?

I’m guessing its not really powershell 1.0. Its confusing that windows continues to call some of the powershell file folders v1. You can know for sure by running $PSVersionTable in powershell. If it complains that is not available then it really is v1 but I find that very hard to believe on 2012R2. The default on that os is v4.

It is using Powershell 3.0

PS C:\Users\selvan> $PSVersionTable

Name Value


PSVersion 3.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.2.9200.16398
PSCompatibleVersions {1.0, 2.0, 3.0}
PSRemotingProtocolVersion 2.2

That powershell script should run ok on v3. Based on the actual error, it is interpreting the array of DNS IPs as a “positional” parameter. This means that powershell is not interpretting it as a value of -ServerAddresses but is trying to infer the parameter from its position on the commandline.

I would check the character encoding of the script. Possibly your dash in front of ServerAddresses is not a normal dash.