powershellbatch-filecmd

Option -Class was not expected


I'm a complete beginner in Windows batch cmd scripting, so maybe this is a trivial question, but Google does not give me an answer.

When I run the following command in cmd or powershell prompt it works without error:

if([bool](Get-WmiObject -Class Win32_Share -ComputerName AMD -Filter "Path='I:\\popsi'") -ne $true)
{
  net share popsi=i:\popsi /GRANT:Igor,FULL
}

When the same command is run in a batch script the error -Class was unexpected at this time is printed. I tried to escape -Class as \-Class but it did not fix the problem. How should the command look in a batch file?


Solution

  • This functionality requires PowerShell. If you need to run it from within a cmd script you can invoke PowerShell with the -command switch like so:

    powershell -c "if (Get-WmiObject -Class Win32_Share -ComputerName AMD -Filter 'Path=''I:\\popsi''') { net share popsi=I:\popsi /GRANT:Igor,FULL }"
    

    Here -c abbreviates -command. It's recommended to type out the switches, I've just cut it short to improve readability. And you don't need to coerce the return value of Get-WmiObject to $true, this is done automatically in the if clause.

    Edit: Fixed the quotation issue.