I'm trying to write a Powershell function to create an OrganizationalUnit. I'm a PS newbie but I've pieced together:
function makeOU ($cn, $path)
{
$sb = [scriptblock]::Create(
"New-ADOrganizationalUnit $cn -path `"$path`"
-ProtectedFromAccidentalDeletion 0"
)
Invoke-Command -ComputerName $server -Credential $Credential `
-ScriptBlock $sb
}
But when I invoke this later in the script, I get a message that -ProtectedFromAccidentalDeletion is an unknown cmdlet. If I make the command one line
"New-ADOrganizationalUnit $cn -path `"$path`" -ProtectedFromAccidentalDeletion 0"
it works.
As I see it, at the end of
"New-ADOrganizationalUnit $cn -path `"$path`"
there is an open parenthesis and an open quote so PS should be looking for more input. Ending that line with a back tick didn't help. Nor did converting the argument to Create() to the form @" ... "@. (This is different from Why I am getting PowerShell Parsing Error? in that I don't have any back ticks, certainly none with spaces after them.)
If I'm making an newbie error here and there's a better way to pass the function parameters to Invoke-Command, I'm open to rewriting but failing that, how can break the string passed to Create() onto multiple lines?