{"id":1288,"date":"2009-03-12T06:00:25","date_gmt":"2009-03-12T14:00:25","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/03\/12\/create-a-remote-file-share\/"},"modified":"2009-03-12T06:00:25","modified_gmt":"2009-03-12T14:00:25","slug":"create-a-remote-file-share","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/03\/12\/create-a-remote-file-share\/","title":{"rendered":"Create A Remote File Share"},"content":{"rendered":"<p>This topic came up recently in the PowerShell forum at <a href=\"http:\/\/www.scriptinganswers.com\" target=\"_blank\">ScriptingAnswers.com<\/a> and I thought you might find it helpful. The issue was creating a file share remotely. In the &#8220;olden days&#8221;, 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.<\/p>\n<p>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.<\/p>\n<div style=\"border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4\">\n<div style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">Function New-FileShare {<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #008000\">#this function returns $True is the share is successfully created<\/span><\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    Param([string]$computername=$env:computername,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">          [string]$path=$(Throw <span style=\"color: #006080\">\"You must enter a complete path relative to the remote computer.\"<\/span>),<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">          [string]$share=$(Throw <span style=\"color: #006080\">\"You must enter the name of the new share.\"<\/span>),<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">          [string]$comment,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">          [int]$connections<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">          )<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">          <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    $FILE_SHARE = 0<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">&nbsp;<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    <span style=\"color: #0000ff\">if<\/span> (! $connections) {<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        <span style=\"color: #008000\">#if no connection number specifed set to $Null so<\/span><\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        <span style=\"color: #008000\">#share will be created with maximum number of connections<\/span><\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        [void]$connections = $NULL<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        }<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    <span style=\"color: #008000\">#uncomment this next line for debugging<\/span><\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #008000\">#     Write-Host (\"Creating share {0} for {1} on {2} ({3})\" -f $share,$path,$computername,$comment) -fore Green<\/span><\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    [wmiclass]$wmishare=<span style=\"color: #006080\">\"\\\\$computername\\root\\cimv2:win32_share\"<\/span><\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    $<span style=\"color: #0000ff\">return<\/span>=$wmishare.Create($path,$share,$FILE_SHARE,$connections,$comment)<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    Switch ($<span style=\"color: #0000ff\">return<\/span>.returnvalue) {<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        0 {$rvalue = <span style=\"color: #006080\">\"Success\"<\/span>}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        2 {$rvalue = <span style=\"color: #006080\">\"Access Denied\"<\/span>}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        8 {$rvalue = <span style=\"color: #006080\">\"Unknown Failure\"<\/span>}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        9 {$rvalue = <span style=\"color: #006080\">\"Invalid Name\"<\/span>}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        10 {$rvalue = <span style=\"color: #006080\">\"Invalid Level\"<\/span>}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        21 {$rvalue = <span style=\"color: #006080\">\"Invalid Parameter\"<\/span>}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        22 {$rvalue = <span style=\"color: #006080\">\"Duplicate Share\"<\/span>}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        23 {$rvalue = <span style=\"color: #006080\">\"Redirected Path\"<\/span>}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        24 {$rvalue = <span style=\"color: #006080\">\"Unknown Device or Directory\"<\/span>}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        25 {$rvalue = <span style=\"color: #006080\">\"Net Name Not Found\"<\/span>}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    }<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    <span style=\"color: #0000ff\">if<\/span> ($<span style=\"color: #0000ff\">return<\/span>.returnvalue <span style=\"color: #cc6633\">-ne<\/span> 0) {<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        Write-Warning (<span style=\"color: #006080\">\"Failed to create share {0} for {1} on {2}. Error: {3}\"<\/span> -f $share,$path,$computername,$rvalue) <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">        <span style=\"color: #0000ff\">return<\/span> $False<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">    }<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    <span style=\"color: #0000ff\">else<\/span> {<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">        <span style=\"color: #0000ff\">return<\/span> $True<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">    }<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">}<\/pre>\n<\/div>\n<\/div>\n<p>The function has the following parameters:<\/p>\n<ul>\n<li>&#8211;<em>computername<\/em> The name of the computer to host the share<\/li>\n<li>&#8211;<em>path<\/em> The folder path to share on the remote computer<\/li>\n<li>&#8211;<em>share<\/em> The name of the share to create. Remember you can append a $ to the name to make it a hidden share.<\/li>\n<li>&#8211;<em>comment<\/em> If you&#8217;d like to create a comment or description for the share<\/li>\n<li>&#8211;<em>connections<\/em> Specify the number of allowed concurrent connections. If you omit this parameter the share will be created to allow the maximum number of connections possible.<\/li>\n<\/ul>\n<p>Here&#8217;s an example of how I might call this function:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">PS C:\\&gt; New-FileShare -computername &#8220;FILE01&#8221; -path d:\\Files -share &#8220;XFiles&#8221; -comment &#8220;Secret Stuff&#8221;<\/font><\/p>\n<p>Let me walk you through quickly how it works. The first major task is to create an instance of the Win32_Share class on the remote computer using the [wmiclass] type adapter.<\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">[wmiclass]$wmishare=\\\\$computername\\root\\cimv2:win32_share<\/font><\/p>\n<p>Now I can call the class&#8217; <strong>Create()<\/strong> method to create a new share.<\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">$return=$wmishare.Create($path,$share,$FILE_SHARE,$connections,$comment) <\/font><\/p>\n<p>The method will write a result object to the pipeline which I&#8217;ll capture as $return. The method has a few more parameters which you can learn about by reviewing the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394435(VS.85).aspx\" target=\"_blank\">documentation<\/a> for this class.<\/p>\n<p>The method will write a return code that indicates if the share creation was successful or not. I use a Switch construct to decode the return value.<\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">Switch ($return.returnvalue) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 {$rvalue = &#8220;Success&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2 {$rvalue = &#8220;Access Denied&#8221;}&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8 {$rvalue = &#8220;Unknown Failure&#8221;}&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9 {$rvalue = &#8220;Invalid Name&#8221;}&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10 {$rvalue = &#8220;Invalid Level&#8221;}&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 21 {$rvalue = &#8220;Invalid Parameter&#8221;}&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 22 {$rvalue = &#8220;Duplicate Share&#8221;}&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 23 {$rvalue = &#8220;Redirected Path&#8221;}&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24 {$rvalue = &#8220;Unknown Device or Directory&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 25 {$rvalue = &#8220;Net Name Not Found&#8221;}<br \/>&nbsp;&nbsp;&nbsp; }<\/font><\/p>\n<p>If the return value is anything other than 0, a warning message is displayed with the decoded return value.<\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">if ($return.returnvalue -ne 0) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Warning (&#8220;Failed to create share {0} for {1} on {2}. Error: {3}&#8221; -f $share,$path,$computername,$rvalue) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $False<\/font><\/p>\n<p>The function will then return $False. Otherwise it will return $True. <\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">else {<br \/>&nbsp;&nbsp;&nbsp;&nbsp; return $True<br \/> }<\/font><\/p>\n<p>This allows you to create a share and only do something else if it was created.<\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">If (New-FileShare -computername &#8220;FILE01&#8221; -path d:\\Files -share &#8220;XFiles&#8221; -comment &#8220;Secret Stuff&#8221;) {<\/font><\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">#do something else<\/font><\/p>\n<p><font face=\"Consolas\" color=\"#800080\" size=\"2\">}<\/font><\/p>\n<p>The newly created file share will use the default Share and NTFS permissions. Sure, you can modify them but I&#8217;ll leave that for you and a future post.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:798f86b9-be94-45bb-9aaa-cd281688a191\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<p>Download the <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/03\/new-fileshare.txt\" target=\"_blank\">function.<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;olden days&#8221;, 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.<\/p>\n<p>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.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,25],"tags":[28,57,466,35,467],"class_list":["post-1288","post","type-post","status-publish","format-standard","hentry","category-howto","category-windows-powershell","tag-powershell","tag-scripting","tag-share","tag-wmi","tag-wmiclass"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1288"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1288\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}