PoshCode Archive  Check-in [17570dacb7]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:This topic came up recently in the PowerShell forum at ScriptingAnswers.com and I thought you might find it helpful. The issue was creating a file share remotely. In the olden days, this would have meant using a command line tool like RMTSHARE, which is really just fine. But if you have a larger workflow, doing it natively in PowerShell may be more desirable.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:17570dacb71e2ce1f49cd476186f831cabc0088aaa269ef62de6fd53bb7d1009
User & Date: jeffhicks 2018-06-10 14:27:10
Context
2018-06-10
14:27
Sending Mails via Powershell with Text Edit, Signature Support and Encryption. check-in: f99eda9e9c user: Patrick tags: trunk
14:27
This topic came up recently in the PowerShell forum at ScriptingAnswers.com and I thought you might find it helpful. The issue was creating a file share remotely. In the olden days, this would have meant using a command line tool like RMTSHARE, which is really just fine. But if you have a larger workflow, doing it natively in PowerShell may be more desirable. check-in: 17570dacb7 user: jeffhicks tags: trunk
14:27
(Requires V2 CTP3) check-in: a4d2fb37ae user: halr9000 tags: trunk
Changes

Added New-FileShare.ps1.























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# encoding: ascii
# api: powershell
# title: New-FileShare
# description: This topic came up recently in the PowerShell forum at ScriptingAnswers.com and I thought you might find it helpful. The issue was creating a file share remotely. In the “olden days”, this would have meant using a command line tool like RMTSHARE, which is really just fine. But if you have a larger workflow, doing it natively in PowerShell may be more desirable.
# version: 0.1
# type: function
# author: jeffhicks
# license: CC0
# function: New-FileShare
# x-poshcode-id: 935
# x-archived: 2015-05-07T02:29:19
# x-published: 2009-03-12T08:04:00
#
# Let me show you how easy it is to create a file share using Windows Management Instrumentation (WMI). I wrote a function called New-FileShare.
#
Function New-FileShare {
#this function returns $True is the share is successfully created
    Param([string]$computername=$env:computername,
          [string]$path=$(Throw "You must enter a complete path relative to the remote computer."),
          [string]$share=$(Throw "You must enter the name of the new share."),
          [string]$comment,
          [int]$connections
          )
          
    $FILE_SHARE = 0
 
    if (! $connections) {
        #if no connection number specifed set to $Null so
        #share will be created with maximum number of connections
        [void]$connections = $NULL
        }
    #uncomment this next line for debugging
#     Write-Host ("Creating share {0} for {1} on {2} ({3})" -f $share,$path,$computername,$comment) -fore Green
    
    [wmiclass]$wmishare="\\$computername\root\cimv2:win32_share"
    
    $return=$wmishare.Create($path,$share,$FILE_SHARE,$connections,$comment)
    
    Switch ($return.returnvalue) {
        0 {$rvalue = "Success"}
        2 {$rvalue = "Access Denied"}     
        8 {$rvalue = "Unknown Failure"}     
        9 {$rvalue = "Invalid Name"}     
        10 {$rvalue = "Invalid Level"}     
        21 {$rvalue = "Invalid Parameter"}     
        22 {$rvalue = "Duplicate Share"}     
        23 {$rvalue = "Redirected Path"}     
        24 {$rvalue = "Unknown Device or Directory"}
        25 {$rvalue = "Net Name Not Found"}
    }
    
    if ($return.returnvalue -ne 0) {
        Write-Warning ("Failed to create share {0} for {1} on {2}. Error: {3}" -f $share,$path,$computername,$rvalue) 
        return $False
    }
    else {
        return $True
    }
}