Düzenle

Aracılığıyla paylaş


Create method of the Win32_Share class

The Create WMI class method initiates sharing for a server resource.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

uint32 Create(
  [in]           string                   Path,
  [in]           string                   Name,
  [in]           uint32                   Type,
  [in, optional] uint32                   MaximumAllowed,
  [in, optional] string                   Description,
  [in, optional] string                   Password,
  [in, optional] Win32_SecurityDescriptor Access
);

Parameters

Path [in]

Local path of the Windows share.

Example, "C:\Program Files".

Name [in]

Passes the alias to a path set up as a share on a computer system running Windows.

Example, "public".

Type [in]

Passes the type of resource being shared. Types includes disk drives, print queues, interprocess communications (IPC), and general devices. Can be one of the following values.

Disk Drive (0)

Print Queue (1)

Device (2)

IPC (3)

Disk Drive Admin (2147483648)

Print Queue Admin (2147483649)

Device Admin (2147483650)

IPC Admin (2147483651)

MaximumAllowed [in, optional]

Limit on the maximum number of users allowed to concurrently use this resource.

Example: 10. This parameter is optional.

Description [in, optional]

Optional comment to describe the resource being shared. This parameter is optional.

Password [in, optional]

Password (when the server is running with share-level security) for the shared resource. If the server is running with user-level security, this parameter is ignored. This parameter is optional.

Access [in, optional]

Security descriptor for user level permissions. A security descriptor contains information about the permissions, owner, and access capabilities of the resource. If this parameter is not supplied or is NULL, then Everyone has read access to the share. For more information, see Win32_SecurityDescriptor and Changing Access Security on Securable Objects.

Return value

Returns one of the values listed in the following list, or any other value to indicate an error. For additional error codes, see WMI Error Constants or WbemErrorEnum. For general HRESULT values, see System Error Codes.

Success (0)

Access denied (2)

Unknown failure (8)

Invalid name (9)

Invalid level (10)

Invalid parameter (21)

Duplicate share (22)

Redirected path (23)

Unknown device or directory (24)

Net name not found (25)

Other (26 4294967295)

Remarks

Create is a static method.

Only members of the Administrators or Account Operators local group or those with Communication, Print, or Server operator group membership can successfully execute Create. The Print operator can only add printer queues. The Communication operator can only add communication device queues.

Examples

The following PowerShell code creates a share.

# create pointer to class
$comp=[WMICLASS]"Win32_share"

# create a new share
$comp.create("c:\","mynewshare",0)

# see results
gwmi win32_share 

The previous code sample returns the following:

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

Name        : ADMIN$
Path        : C:\Windows
Description : Remote Admin

Name        : C$
Path        : C:\
Description : Default share

Name        : CCMLOGS$
Path        : C:\Windows\CCM\Logs
Description : Public Share

Name        : ccmsetup$
Path        : C:\Windows\ccmsetup
Description : Public Share

Name        : Drop
Path        : C:\Drop
Description : 

Name        : IPC$
Path        : 
Description : Remote IPC

Name        : Share
Path        : C:\Share
Description : 

The following C# code sample describe how to call the create method.

private static void makeShare(string servername, string filepath, string sharename)
{
try
 {
 // assemble the string so the scope represents the remote server
 string scope = string.Format("\\\\{0}\\root\\cimv2", servername);

 // connect to WMI on the remote server
 ManagementScope ms = new ManagementScope(scope);

 // create a new instance of the Win32_Share WMI object
 ManagementClass cls = new ManagementClass("Win32_Share");

 // set the scope of the new instance to that created above
 cls.Scope = ms;

 // assemble the arguments to be passed to the Create method
 object[] methodargs = { filepath, sharename, "0" };

 // invoke the Create method to create the share
 object result = cls.InvokeMethod("Create", methodargs);
 }
catch (SystemException e)
 {
  Console.WriteLine("Error attempting to create share {0}:", sharename);
  Console.WriteLine(e.Message);
 }

}

Requirements

Requirement Value
Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Namespace
Root\CIMV2
MOF
CIMWin32.mof
DLL
CIMWin32.dll

See also

Operating System Classes

Win32_Share