Link to home
Start Free TrialLog in
Avatar of Zack
ZackFlag for Australia

asked on

Powershell - Cannot find an overload for ".ctor" and the argument count: "1".

Hi EE,

I am running the following script (the script function is essentially to refresh databases from prod to UAT environments) see attached with the following command:

./Invoke-Edited.ps1 -Environment Test -ScriptBlock {param($p1, $p2)} -ArgumentList $ComputerName, $SQLBackupPath

I am running the scripts remotely and I am getting the following error

Cannot find an overload for ".ctor" and the argument count: "1".
At C:\Invoke-Edited.ps1:54 char:9
+         [ValidateNotNullOrEmpty('DC1PRDSQLFCI02')]

Any assistance is appreciated.

Thank you.
Edited.ps1
SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zack

ASKER

Hi Jose,

Okay if want to set the parameter value in the form of the pre-set variable can I use something like this? :

$ComputerName = "DC1PRDSQLFCI1"
        $SQLBackupPath = '\\DC1PRDSQLFCI1\Z$\MSSQL13.SQL2016APPS01\MSSQL\Backup\DC1PRDSQLFCI1$SQL2016APPS\PeopleEnt\FULL'

        [Parameter(Mandatory=$true,
				   ValueFromPipeline=$True,
				   ValueFromPipelineByPropertyName=$true,
				   HelpMessage="Select PeoplePoint environment")]
		[ValidateNotNullOrEmpty()]
		[ValidateSet("Development", "Test", "Training")]
		[String]$Environment,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ComputerName,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$SQLBackupPath
        

Open in new window


Thank you.
Sorry  Zack I didn't understand.

if you want to set a parameter value the way is:
Remove the Mandatory parameter (from $true), so that makes it mandatory=$false...

[Parameter(Mandatory=$false,Position=0)] 
$ComputerName = "DC1PRDSQLFCI1"

Open in new window

Avatar of Zack

ASKER

Hi Jose,

That's fine thank you very much for your assistance: I am stuck with this issue now;

Command: PS C:\> ./Invoke-Edited.ps1 -Environment Test -ScriptBlock {param($p1, $p2)} -ArgumentList $Comput
erName, $SQLBackupPath

A parameter cannot be found that matches parameter name 'ScriptBlock'.

What I'm trying to do is parse execute $ComputerName and SQLBackupPathName remotely on the target box.

If don't parse these variables to execute remotely, I get this error. Cannot bind argument to parameter 'Path' because it is null.

My apologies if my query is opaque.

Thank you.
You'd need to defined the scriptblock:

like this:
$sb = { 
param(
[Parameter(mandatory=$true,position=0)]$A,
[Parameter(mandatory=$true,position=1)]$B
)
    Write-Host "C: $($A+$B)"
}

Invoke-Command -ScriptBlock $sb -ArgumentList 1,3

Open in new window


Your ScriptBlock only receives 2 parameters ($p1,$p2) and DOES NOTHING :) hahaha
Avatar of Zack

ASKER

Hi Jose,

Okay, but how can nest $sb inside the existing code structure:

 
param
	(
   

        [Parameter(Mandatory=$true,
				   ValueFromPipeline=$True,
				   ValueFromPipelineByPropertyName=$true,
				   HelpMessage="Select PeoplePoint environment")]
		[ValidateNotNullOrEmpty()]
		[ValidateSet("Development", "Test", "Training")]
		[String]$Environment,

        #[Parameter(Mandatory=$false,Position=0)] 
        #$ComputerName = ("DC1PRDSQLFCI1"),

       # [Parameter(Mandatory=$false,Position=1)] 
       # $SQLBackupPath = ('\\DC1PRDSQLFCI1\Z$\MSSQL13.SQL2016APPS\MSSQL\Backup\DC1PRDSQLFCI02$SQL2016APPS\PeopleEntProd\FULL')


          $ComputerName = ("DC1PRDSQLFCI1"),
          $SQLBackupPath = ('\\DC1PRDSQLFCI1\Z$\MSSQL13.SQL2016APPS\MSSQL\Backup\DC1PRDSQLFCI02$SQL2016APPS\PeopleEntProd\FULL')
        $sb = { 
        param(
        [Parameter(mandatory=$true,position=0)]$ComputerName
        ,
        [Parameter(mandatory=$true,position=1)]$SQLBackupPath
        )
        Write-Host "C: $($ComputerName+$SQLBackupPath)"
    }
    )

Open in new window


I am not so sure that PowerShell allows for this can you clarify?

Thank you.
Avatar of Zack

ASKER

Hi Jose,

To clarify in the -Environment variable needs to be entered in manually by the user but the $ComputerName + $SQLBackupPath are to be fixed specified in the ps1 script and to be executed remotely.

Thank you.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zack

ASKER

Hi Jose,

Fair point and thank you for your assistance I will begin to pay around with this script and see if I can get it working.