Create shared folder with [WMICLASS]"Win32_Share"

958 views Asked by At

I'm using a PowerShell script that creates local share folder.

function shareCacheFolder() {
  $Sharename = 'cacheFolder'
  $Foldername = 'c:\cacheFolder'
  if (!(Get-WmiObject Win32_Share -Filter "name='$Sharename'")) {
    $Shares = [WMICLASS]"Win32_Share"
    $Shares.Create($Foldername, $Sharename, 0)
  }
}

This seems to work on most machines. But not on mine.

I'm getting this output:

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     : 
__DYNASTY        : __PARAMETERS
__RELPATH        : 
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
ReturnValue      : 8
PSComputerName   : 

I've also tried using this convention:

function shareCacheFolder() {
  $path = "c:\cachefolder"
  $name = "cachefolder"
  $type = 0
  $password = ""
  $description = ""
  $max = 100
  $access = $null
  if (!(Get-WmiObject Win32_Share -Filter "name='$Sharename'")) {
    Invoke-WmiMethod -Class Win32_Share -Name Create -ArgumentList $access, $description, $max, $name, $password, $path, $type
  }
}

I'm using PowerShell version 3.

1

There are 1 answers

1
Ansgar Wiechers On

As documented the return code 8 means "unknown error". The command is syntactically correct and works on other computers, so the issue is not with your code. The problem is also unlikely to be caused by a share name conflict, missing folder, permission issue, etc., as those would cause different errors/return codes.

I suspect that on your system the Server service is not running. Verify that with the following command:

Get-Service 'Server' | Select-Object StartType, Status | Format-List

The start type should be "Automatic", and the status should be "Running". If they're not, change the start type and start the service like this:

Get-Service 'Server' | Set-Service -StartupType 'Automatic'
Start-Service 'Server'