This module provides useful WSH (Windows Script Host) functions that handle SMB (Server Message Block), CIFS(Common Internet File System) on Windows.
Requires
- module:./Net.js
Members
(static, constant) IPC :string
Windows system-specific end-of-line marker. Similar to Node.js OS.
Type:
- string
Methods
(static) connect(comp, shareNameopt, domainopt, useropt, pwdopt, optionsopt) → {0|string}
Asynchronously connects the Windows to the network shared resource.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.connect('MyNas', null, null, 'nas-user1', 'usrP@ss');
// Returns a number immediately and asynchronously executes:
// `net.exe use \\\\MyNas\\IPC$ usrP@ss /user:nas-user1`
smb.connect('11.22.33.44', 'public', 'PCNAME', 'UserId', 'usrP@ss');
// Returns a number immediately and asynchronously executes:
// `net.exe use \\\\11.22.33.44\\public usrP@ss /user:PCNAME\UserId`
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
comp |
string | The computer name or IP address. |
|||||||||||||||||
shareName |
string |
<optional> |
'IPC$'
|
The share name. |
|||||||||||||||
domain |
string |
<optional> |
''
|
The domain name. If it is empty, uses the current logged on domain. |
|||||||||||||||
user |
string |
<optional> |
The user name with which to log on. |
||||||||||||||||
pwd |
string |
<optional> |
The password. @TODO If * produce a prompt for the password. |
||||||||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- If options.isDryRun is true, returns string.
- Type
- 0 | string
(static) connectSync(comp, shareNameopt, domainopt, useropt, pwdopt, optionsopt) → {object|string}
Synchronously connects the Windows to the network shared resource.
Example
var smb = Wsh.Net.SMB; // Shorthand
var retObj = smb.connectSync('MyNas', null, null, 'nas-user1', 'usrP@ss');
// Synchronously executes:
// `net.exe use \\\\MyNas\\IPC$ usrP@ss /user:nas-user1`
// Returns: { error: false, stdout: '...', stderr: '' }
var retObj = smb.connectSync('11.22.33.44', 'public', 'PCNAME', 'UserId', 'usrP@ss');
// Synchronously Executes:
// `net.exe use \\\\11.22.33.44\\public usrP@ss /user:PCNAME\UserId`
// Returns: { error: false, stdout: '...', stderr: '' }
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
comp |
string | The computer name. |
|||||||||||||||||
shareName |
string |
<optional> |
'IPC$'
|
The share name. |
|||||||||||||||
domain |
string |
<optional> |
''
|
The domain name. If it is empty, uses the current logged on domain. |
|||||||||||||||
user |
string |
<optional> |
The user name with which to log on. |
||||||||||||||||
pwd |
string |
<optional> |
The password. *: produce a prompt for the password. |
||||||||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- See typeRunSyncReturn. If options.isDryRun is true, returns string.
- Type
- object | string
(static) connectSyncSurely(comp, shareNameopt, domainopt, useropt, pwdopt, optionsopt) → {void|string}
Connects the Windows to the network shared resource. The following points are different from connectSync. (1). If you already have the connection, do nothing. (2). Connect, and if error 1219 is returned, disconnect and reconnect.
Example
var smb = Wsh.Net.SMB; // Shorthand
var retObj = smb.connectSyncSurely('MYPC1234', 'C$', null, 'UserId', 'usrP@ss');
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
comp |
string | The computer name. |
||||||||||||
shareName |
string |
<optional> |
'IPC$'
|
The share name. |
||||||||||
domain |
string |
<optional> |
''
|
The domain name. If it is empty, uses the current logged on domain. |
||||||||||
user |
string |
<optional> |
The user name with which to log on. |
|||||||||||
pwd |
string |
<optional> |
The password. *: produce a prompt for the password. |
|||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- If options.isDryRun is true, returns string.
- Type
- void | string
(static) delSharedDirectory(shareName, optionsopt) → {object|string}
[Requires admin rights] Deletes the share directory in this PC.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.delSharedDirectory('MyShareName');
// Returns a result object executed `net share` command.
Parameters:
| Name | Type | Attributes | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
shareName |
string | The share name to delete. |
||||||||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- See typeRunSyncReturn. If options.isDryRun is true, returns string.
- Type
- object | string
(static) disconnect(compopt, shareNameopt, optionsopt) → {0|string}
Asynchronously disconnects the Windows from the network shared resource.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.disconnect('11.22.33.44', 'C$');
// Returns 0 immediately and asynchronously executes:
// `net.exe use \\\\11.22.33.44\\C$ /delete`
Parameters:
| Name | Type | Attributes | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
comp |
string |
<optional> |
The computer name or IP address. * is all |
|||||||||||||||
shareName |
string |
<optional> |
The share Name |
|||||||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- If options.isDryRun is true, returns string.
- Type
- 0 | string
(static) disconnectSync(compopt, shareNameopt, optionsopt) → {object|string}
Synchronously disconnects the Windows from the network shared resource.
Example
var smb = Wsh.Net.SMB; // Shorthand
var retObj = smb.disconnectSync('11.22.33.44', 'public');
// Synchronously executes:
// `net.exe use \\\\11.22.33.44\\public /delete`
// Returns: { error: false, stdout: '...', stderr: '' }
Parameters:
| Name | Type | Attributes | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
comp |
string |
<optional> |
The computer name. * is all |
||||||||||
shareName |
string |
<optional> |
The share Name |
||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- See typeRunSyncReturn. If options.isDryRun is true, returns string.
- Type
- object | string
(static) existsShareName(shareName) → {boolean}
Checks whether the shared name is defined.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.existsShareName('MyShareName'); // Returns: true
Parameters:
| Name | Type | Description |
|---|---|---|
shareName |
string | The shared name to check. |
Returns:
- If defined, returns true.
- Type
- boolean
(static) getActiveConnectionsObjs(matchedopt) → {Array.<typeWin32NetworkConnectionClassProps>}
[REQUIRE ADMIN] Gets an Array of objects of network connections on the Windows.
Example
var smb = Wsh.Net.SMB; // Shorthand
var connections = smb.getActiveConnectionsObjs();
console.dir(connections);
// Outputs: [
// {
// AccessMask: 1179785,
// Caption: "RESOURCE CONNECTED",
// Comment: " ",
// ConnectionState: "Connected",
// ConnectionType: "Current Connection",
// Description: "RESOURCE CONNECTED - Microsoft Windows Network",
// DisplayType: "Share",
// InstallDate: null,
// LocalName: null,
// Name: "\\MyNas\Public",
// Persistent: false,
// ProviderName: "Microsoft Windows Network",
// RemoteName: "\\MyNas\Public",
// RemotePath: "\\MyNas\Public",
// ResourceType: "Disk",
// Status: "OK",
// UserName: "MyNas\UserName"
// }, {
// ...
// ..
// }]
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
matched |
string |
<optional> |
The Regular expression that matches the Name property. e.g. '^\\\\MyNas' |
Returns:
- An Array of the network connections objects.
- Type
- Array.<typeWin32NetworkConnectionClassProps>
(static) getActiveConnectionsSwbemObjs(matchedopt) → {Array.<sWbemObjectSet>}
[REQUIRE ADMIN] Gets an Array of Enumerated-SWbemObjectSet of network connections on the Windows. See Win32_NetworkConnection class.
Example
var smb = Wsh.Net.SMB; // Shorthand
var swbemObjs = smb.getActiveConnectionsSwbemObjs();
// Returns: An Array of Win32_NetworkConnection class.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
matched |
string |
<optional> |
The Regular expression that matches the Name property. e.g. '^\\\\MyNas' |
Returns:
- Enumerated SWbemObjectSets. See Win32_NetworkConnection class.
- Type
- Array.<sWbemObjectSet>
(static) getLocalSharesObjs() → {Array.<typeWin32ShareClassProps>}
Gets an Array of the shared resource objects on the Windows.
Example
var smb = Wsh.Net.SMB; // Shorthand
var resources = smb.getLocalSharesObjs();
console.dir(resources);
// Outputs: [
// {
// AccessMask: null,
// AllowMaximum: true,
// Caption: "Remote Admin",
// Description: "Remote Admin",
// InstallDate: null,
// MaximumAllowed: null,
// Name: "ADMIN$",
// Path: "C:\WINDOWS",
// Status: "OK",
// Type: -2147483648
// }, {
// ...
// ..
// }, {
// AccessMask: null,
// AllowMaximum: true,
// Caption: "shareDirectory",
// Description: "shareDirectory",
// InstallDate: null,
// MaximumAllowed: null,
// Name: "shareDirectory",
// Path: "C:\Users\UserName\AppData\Local\Temp\shareDirectory_testDir",
// Status: "OK",
// Type: 0 }]
Returns:
- An Array of the shared resource objects.
- Type
- Array.<typeWin32ShareClassProps>
(static) getLocalSharesSWbemObjs() → {Array.<sWbemObjectSet>}
Gets an Array of Enumerated-SWbemObjectSet of shared resources on the Windows. See Win32_Share class.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.getLocalSharesSWbemObjs();
// Returns: An Array of Win32_Share class.
Returns:
- Enumerated SWbemObjectSets. See Win32_Share class.
- Type
- Array.<sWbemObjectSet>
(static) hasConnection(connectionNameopt) → {boolean}
[REQUIRE ADMIN]
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.hasConnection('\\\\192.168.12.34\\myPrivate'); // Returns: true
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
connectionName |
string |
<optional> |
The connection Name. |
Returns:
- Type
- boolean
(static) shareDirectory(shareName, dirPath, optionsopt) → {object|string}
[Requires admin rights] Shares the directory of this Windows.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.shareDirectory('MyShareName', 'D:\\sharedFolder', {
grant: 'READ',
remark: 'Share for example'
});
// Returns a result object executed `net share` command.
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
shareName |
string | The share name. |
|||||||||||||||||||||||||||||||
dirPath |
string | The directory path to share. |
|||||||||||||||||||||||||||||||
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- See typeRunSyncReturn. If options.isDryRun is true, returns string.
- Type
- object | string
(static) showCurrentSession(optionsopt) → {void}
Shows the current network session on a window.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.showCurrentSession(); // Shows a `net use` window
Parameters:
| Name | Type | Attributes | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- Type
- void
(static) showLocalShares(optionsopt) → {void}
Shows a window of the local shared folders.
Example
var smb = Wsh.Net.SMB; // Shorthand
smb.showLocalShares(); // Shows a window
Parameters:
| Name | Type | Attributes | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
<optional> |
Optional parameters. Properties
|
Returns:
- Type
- void