Share via


Problem with powershell arguments

Question

Tuesday, January 26, 2016 2:28 PM

When i run my powershell script from a DOS-prompt my dropdown list is wrong.

%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File OULocationPrompt.ps1 -RBOptionFirst Workstation -RBOptionSecond Server -DomainName blabla -DomainSuffix local -LocationList @("Desktops","Laptops")

Results in dropdown @(Desktops,Laptops)

When i run my script straight tru powershell. My results are good in my dropdown list.

.\OULocationPrompt.ps1 -RBOptionFirst Workstation -RBOptionSecond Server -DomainName blabla -DomainSuffix local -LocationList @("Desktops","Laptops")

Results in dropdow:

Desktops

Laptops

Can anyone help me out. Why is it different?

Powershell script is here:

param(
[parameter(Mandatory=$true)]
[string]$RBOptionFirst,
[parameter(Mandatory=$true)]
[string]$RBOptionSecond,
[parameter(Mandatory=$true)]
[string]$DomainName,
[parameter(Mandatory=$true)]
[string]$DomainSuffix,
[parameter(Mandatory=$true)]
[string[]]$LocationList
)
 
function Load-Form {
    $Form.Controls.AddRange(@($RBOption1, $RBOption2, $ComboBox, $Button, $GBSystem, $GBLocation))
    $ComboBox.Items.AddRange($LocationList)
    $Form.Add_Shown({$Form.Activate()})
    [void]$Form.ShowDialog()
}
 
function Set-OULocation {
    param(
    [parameter(Mandatory=$true)]
    $Location
    )
    if ($RBOption1.Checked -eq $true) {
        $OULocation = "LDAP://OU=$($RBOptionFirst),OU=$($Location),DC=$($DomainName),DC=$($DomainSuffix)"
    }
    if ($RBOption2.Checked -eq $true) {
        $OULocation = "LDAP://OU=$($RBOptionSecond),OU=$($Location),DC=$($DomainName),DC=$($DomainSuffix)"
    }
    $TSEnvironment = New-Object -COMObject Microsoft.SMS.TSEnvironment 
    $TSEnvironment.Value("OSDDomainOUName") = "$($OULocation)"
    $Form.Close()
}
 
# Assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
 
# Form
$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(260,220)  
$Form.MinimumSize = New-Object System.Drawing.Size(260,220)
$Form.MaximumSize = New-Object System.Drawing.Size(260,220)
$Form.SizeGripStyle = "Hide"
$Form.StartPosition = "CenterScreen"
$Form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($PSHome + "\powershell.exe")
$Form.Text = "Choose a location"
$Form.ControlBox = $false
$Form.TopMost = $true
 
# Group boxes
$GBSystem = New-Object System.Windows.Forms.GroupBox
$GBSystem.Location = New-Object System.Drawing.Size(10,10)
$GBSystem.Size = New-Object System.Drawing.Size(220,60)
$GBSystem.Text = "Select system"
$GBLocation = New-Object System.Windows.Forms.GroupBox
$GBLocation.Location = New-Object System.Drawing.Size(10,80)
$GBLocation.Size = New-Object System.Drawing.Size(220,60)
$GBLocation.Text = "Select location"
 
# Radio buttons
$RBOption1 = New-Object System.Windows.Forms.RadioButton
$RBOption1.Location = New-Object System.Drawing.Size(20,33)
$RBOption1.Size = New-Object System.Drawing.Size(100,20)
$RBOption1.Text = "$($RBOptionFirst)"
$RBOption1.Add_MouseClick({$ComboBox.Enabled = $true})
$RBOption2 = New-Object System.Windows.Forms.RadioButton
$RBOption2.Location = New-Object System.Drawing.Size(120,33)
$RBOption2.Size = New-Object System.Drawing.Size(100,20)
$RBOption2.Text = "$($RBOptionSecond)"
$RBOption2.Add_MouseClick({$ComboBox.Enabled = $true})
 
# Combo Boxes
$ComboBox = New-Object System.Windows.Forms.ComboBox
$ComboBox.Location = New-Object System.Drawing.Size(20,105)
$ComboBox.Size = New-Object System.Drawing.Size(200,30)
$ComboBox.DropDownStyle = "DropDownList"
$ComboBox.Add_SelectedValueChanged({$Button.Enabled = $true})
$ComboBox.Enabled = $false
 
# Buttons
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(140,145)
$Button.Size = New-Object System.Drawing.Size(80,25)
$Button.Text = "OK"
$Button.Enabled = $false
$Button.Add_Click({Set-OULocation -Location $ComboBox.SelectedItem.ToString()})
 
# Load Form
Load-Form

All replies (12)

Tuesday, January 26, 2016 4:22 PM ✅Answered | 1 vote

Use -EncodedCommand:

https://technet.microsoft.com/en-us/library/hh847736.aspx


Tuesday, January 26, 2016 6:36 PM ✅Answered | 1 vote

$command needs to be a string.

$command = '.\OULocationPrompt.ps1 -RBOptionFirst Werkstations -RBOptionSecond Server -DomainName blabla -DomainSuffix local -LocationList @("Desktops","Laptops")'
$bytes = [Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)

$encodedCommand


Tuesday, January 26, 2016 4:37 PM

So i need to add.

$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($LocationList))

Correct me if im wrong.


Tuesday, January 26, 2016 4:38 PM

Correct me if im wrong.

The whole command should be encoded.


Tuesday, January 26, 2016 4:43 PM

I do not get it. Can you please help me.


Tuesday, January 26, 2016 4:48 PM

I do not get it. Can you please help me.

Show me what you've tried and what your errors are first.


Tuesday, January 26, 2016 5:39 PM

Something like this?

$command = ./OULocationPrompt.ps1 -RBOptionFirst Werkstations -RBOptionSecond Server -DomainName blabla -DomainSuffix local -LocationList @("Desktops","Laptops")
$bytes = [Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -noprofile -encodedCommand $encodedCommand

Tuesday, January 26, 2016 5:51 PM

Looks good, does it work?

Remember, take the output of $encodedCommand and use that in CMD.

powershell.exe -encodedCommand QAAoACIARABlAHMAawB0AG8AcABzACIALAAiAEwAYQBwAHQAbwBwAHMAIgApAA==


Tuesday, January 26, 2016 5:58 PM

There is no output.

Exception calling "GetBytes" with "1" argument(s): "Array cannot be null.
Parameter name: chars"
At \\B****\Sources\OSD\Scripts\test.ps1:2 char:1
+ $bytes = [Text.Encoding]::Unicode.GetBytes($command)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException
 
Exception calling "ToBase64String" with "1" argument(s): "Value cannot be null.
Parameter name: inArray"
At \\B****\Sources\OSD\Scripts\test.ps1:3 char:1
+ $encodedCommand = [Convert]::ToBase64String($bytes)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException
 
powershell.exe : Cannot process the command because of a missing parameter. A command must follow -Command.
At \\B****\Sources\OSD\Scripts\test.ps1:4 char:1
+ powershell.exe -noprofile -encodedCommand $encodedCommand
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Cannot process ...ollow -Command.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Tuesday, January 26, 2016 6:35 PM

I still don't get it.


Tuesday, January 26, 2016 6:47 PM

Thank you very much. But if your encodedcommand is to long. How can you fix that?


Tuesday, January 26, 2016 7:24 PM

You can't, the length it ends up with is the length it needs to be.

As an alternative approach, you could use a caller script that contains all of your parameters and just point to the caller script with the -File parameter of powershell.exe.