也许标题写的不好,这篇文章没什么阅读量。在我看来这篇文章算是我写的所有文章里面涉及的模块最多,深度和实用性都很好的文章。实在有些可惜,改个名字试试。

脚本只能解决特定环境下的特定问题。

这个脚本解决了在工作组环境中,用户终端在专用网络下共享一个文件夹的目的。

它做了以下的事情:

1.设置共享的依赖服务'server'为自动启动,并启动这个服务

2.启用windows防火墙

3.创建一个非特权用户

4.根据提供的参数设置共享,访问权限为只读

5.开启专用网络下的防火墙规则,允许smb流量入站。

6.在桌面生成一个Share.bat,在需要访问的电脑上以管理员身份执行,可以把凭证添加到凭证管理器中。


注意:

注释中的'|'是一个选择符号,表示要么您使用|左边的,要么使用右边的。而不能同时都用。

Powershell脚本的默认执行策略是不允许,所以下面的1,2步为必须


运行的一个例子:

1.以管理员身份运行Powershell

2.PS >set-executionPolicy Bypass -Scope Process

3.PS >.\T.ps1 | .\T.ps1 -ShareName Example -smbpath D:\Example -Des "脚本分享" -UserName Bob -pass {123 | 或者不用这个参数}



注意:set-executionPolicy Bypass -Scope Process 只会修改当前会话中的执行策略,不会改变其它地方的执行策略。

.\T.ps1 不带参数执行。它会在C盘创建一个名为c:\example的文件夹,并以只读的权限共享它。

从安全性角度考虑,如果您不了解脚本做了什么,那么您不应该执行任何脚本。

如果您想要一个设置共享的工具,那么这个脚本可以作为一个参考。

您可以很轻松的调整这个脚本,以满足您自己的需求。

设置共享打印机参考:get-printer -Name "DocuCentre-IV 3065 | 你的打印机" | `

Set-Printer -Shared $True -ShareName "大厅黑白打印机 | 你的共享名"

[CmdletBinding()]
param (
    [Parameter()]
    [String]$ShareName = 'Example',

    [Parameter()]
    [String]$smbpath = 'C:\Example',

    [Parameter(Mandatory = $false)]
    [string]$Des = "个人共享",

    [Parameter()]
    [string]$UserName = 'Bob',

    [Parameter(Mandatory = $false)]
    [string]$pass = 'L^$K5PN3%4cH!vvg'
)

#开启共享服务
try{
    Get-Service -Name server | Set-Service -StartupType Automatic | Start-Service -ErrorAction Stop
}catch{
    throw "The service failed to start!"
}

#检查网络位置,专用环境脚本继续执行,公共或者域环境,停止执行。
Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled True
try{
    Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private -ErrorAction Stop
}catch{
    if((Get-NetConnectionProfile).NetworkCategory -eq "Public"){
        throw "The current network is public, and the script exits"
    }else{
        throw "The current network is a domain, please consult your administrator"
    }
}


#设置共享用户(用户存在修改密码,用户不存在创建新用户)
if(Get-LocalUser $UserName -ErrorAction SilentlyContinue){
    Set-LocalUser -Name $UserName -Password (ConvertTo-SecureString -String $password -AsPlainText -Force) | out-null
}
else{
    new-localuser -Name $UserName -Password (ConvertTo-SecureString -String $password -AsPlainText -Force)`
     -PasswordNeverExpires | Add-LocalGroupMember -Group users | Out-Null
}

#设置共享文件夹(提供的路径文件夹存在,配置共享 | 创建文件夹,配置共享)
if(Test-Path $smbpath)
{
    new-smbshare -Path $smbpath -Description $Des -name $ShareName -ReadAccess $UserName | out-null
}
else{
    try{
        New-Item -Path $smbpath -ItemType Directory -ErrorAction Stop
        new-smbshare -Path $smbpath -Description $Des -name $ShareName -ReadAccess $UserName | out-null
    }catch{
        throw "The path is invalid"
    }
}

#启用smb防火墙规则
Get-NetFirewallRule -Name FPS-SMB-In-TCP,FPS-NB_Session-In-TCP | set-netfirewallrule -Enabled true -Action Allow -Profile Private

#挂载网络路径添加到脚本中
"cmdkey /add:$env:computername /user:$username /pass:$pass" >> $env:homepath\desktop\share.bat
"net use * \\$env:computername\$ShareName" >> $env:homepath\desktop\share.bat