2

I have the following cmd file:-

PowerShell.exe -noexit E:\wwwroot\domains\processes\AddDirectory.ps1 -Param testdomain.co.uk

which goes through to:-

$Session = New-PSSession -ComputerName 192.168.0.25
$script = {
Param($Param1)
set-executionpolicy unrestricted -force

# Set Variables
$domain = $Param1
$sitepath = "e:\domains\" + $domain

# Check for physical path
if (-not (Test-Path -path $sitePath))
{
New-Item -Path $sitepath -type directory 
New-Item -Path $sitepath\wwwroot -type directory 
}
set-executionpolicy restricted -force     
}
Invoke-Command -Session $Session -ScriptBlock $script

But it just runs but does nothing.

If I declare the $domain variable as $domain = 'testdomain.co.uk' it works but it doesn't want to pass through the var from the cmd file. What am I doing wrong? I've tried to put it in the Invoke-Command as -ArgumentsList -$Param1 but that doesn't work either.....

Any ideas greatfully received

Thanks Paul

Update - I've updated my code as per below but getting same issue:-

param($domainName)
$script = {
    Param($Param1)
    set-executionpolicy unrestricted -force
    # Set Variables
    $domain = $Param1
    $sitepath = "e:\domains\" + $domain
    # Check for physical path
    if (-not (Test-Path -path $sitePath))
    {
        New-Item -Path $sitepath -type directory
        New-Item -Path $sitepath\wwwroot -type directory
        New-Item -Path $sitepath\db -type directory
        New-Item -Path $sitepath\stats -type directory
    }
    set-executionpolicy restricted -force
}

$Session = New-PSSession -ComputerName 192.168.0.25

Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
Paul
  • 23
  • 1
  • 4
  • Is e: a local or networked mapped drive. If it is the latter, you may be running into a double-hop authentication issue. – Keith Hill Oct 24 '12 at 01:29

1 Answers1

2

You need to use a param block in the script, the argument you pass to the file will be assign to $domainName and you will use it to pass the value to the scriptblock :

PowerShell.exe -noexit E:\wwwroot\domains\processes\AddDirectory.ps1 testdomain.co.uk


# script file

param($domainName)

$script = {
    Param($Param1)

    ...
    $domain = $Param1
    ...   
}

$Session = New-PSSession -ComputerName 192.168.0.25
Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • ok I've out the following code in now but it doesn't work - the file runs but nothing happens:- – Paul Oct 23 '12 at 19:23
  • param($domainName) $script = { Param($Param1) set-executionpolicy unrestricted -force # Set Variables $domain = $Param1 $sitepath = "e:\domains\" + $domain # Check for physical path if (-not (Test-Path -path $sitePath)) { New-Item -Path $sitepath -type directory New-Item -Path $sitepath\wwwroot -type directory New-Item -Path $sitepath\db -type directory New-Item -Path $sitepath\stats -type directory } set-executionpolicy restricted -force } $Session = New-PSSession -ComputerName 192.168.0.25 Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName – Paul Oct 23 '12 at 19:25
  • Hi - updated code in initial question as it wont let me format correctly in the comments box... – Paul Oct 23 '12 at 20:59
  • This is working now - in my cmd file I had *.ps1 -param testdomain.co.uk. I've taken out the -param and it works - many thanks! – Paul Oct 24 '12 at 09:22