Reputation: 519
I'm getting a null expression when I try to access a variable within the ShouldProcess script-block. This is what I have in a script method:
$scaObject = [PerfWorker.CmdLets.PSDbInfoFactory]::GetPSDbInfo($info, $false)
$oracleObj = [PerfWorker.CmdLets.OracleParamsDto]$scaObject
Add-Member -InputObject $oracleObj -MemberType ScriptMethod -Name DropSchemas -Value {
$oraWorker = [PerfWorker.CmdLets.PSDbOracleInfo]$this.DbWorker
$args | foreach {
#Start getting error "You cannot call a method on a null-valued expression" from line below
if($psCmdLet.ShouldProcess(
"Delete Oracle Schema $_ on $($this.Hostname)? This action cannot be undone!",
"Delete Schema?"))
{
$oraWorker.DropS3DSchemas($_)
}
}
}
If I remove the $psCmdlet.ShouldProcess
block and just call the $oraWorker.DropS3DSchemas()
method, then everything works fine.
Upvotes: 1
Views: 1607
Reputation: 202032
You appear to be mixing the cmdlet execution context with the execution context of a script method on an object. The script method code is captured in a scriptblock that is run later outside the context of the cmdlet method that created it. Before the if
check if $PSCmdlet -eq $null
. I don't think you can do what you're trying to do here. The ShouldProcess()
needs to execute in the context of your cmdlet. The PowerShell way would be to create a Drop-Schemas cmdlet that does the work. Then you can use the ShouldProcess()
feature in that cmdlet.
Upvotes: 3