简体   繁体   English

在PowerShell脚本中运行Net Share命令

[英]Run Net Share command in powershell script

I try to share a folder in powershell with net share command, i can't use group or user name for share permission because this script will be used on the systems with different os languages, for this reason i use group/user SID to set up share permissions. 我尝试使用net share命令在powershell中共享一个文件夹,我不能使用组或用户名来获得共享权限,因为这个脚本将在具有不同操作系统语言的系统上使用,因此我使用组/用户SID来设置分享权限。

Here is my script, my function work great outside of command. 这是我的脚本,我的功能在命令之外很棒。 But my function dont work in "NET SHARE" cmd. 但我的功能不适用于“NET SHARE”cmd。

function Get-GroupName {
param ($SID)

$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$objUser = $objSID.Translate([System.Security.Principal.NTAccount])
$objUser.Value
}

# Share Folder + Set Share Permission SID Based
cmd /c net share MSI=C:\MSI /GRANT:(Get-GroupName -SID 'S-1-1-0'),READ 

This will work: 这将有效:

cmd /c $( "net share MSI=C:\MSI /GRANT:""$(Get-GroupName -SID 'S-1-1-0')"",READ" )

But if you're on Windows 8 or newer, Windows Server 2012 or newer you can use the Set-SmbShare and Grant-SmbShareAccess cmdlets instead: 但是,如果您使用的是Windows 8或更高版本的Windows Server 2012或更高版本,则可以使用Set-SmbShareGrant-SmbShareAccess cmdlet:

http://technet.microsoft.com/en-us/library/jj635727 http://technet.microsoft.com/en-us/library/jj635727

http://technet.microsoft.com/en-us/library/jj635705 http://technet.microsoft.com/en-us/library/jj635705

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Python脚本运行PowerShell函数 - Run PowerShell function from Python script 在 python 脚本内运行 powershell 脚本 - Run powershell Scripts INSIDE python script 使用“使用PowerShell运行”执行时,在另一个脚本中调用函数 - Call a function in another script when executing using 'Run With PowerShell' 如何通过Powershell从远程计算机异步运行Python脚本? - How to Run Python Script from Remote Machine via Powershell Asynchronously? PowerShell 从 Windows 表单 add_click 运行“函数”(不是脚本) - PowerShell run a 'function' (not a script) from a Windows Form add_click run_command()的作用是什么[echo“ +”“ $ @”; “$ @”; 在bash脚本中意味着什么? - What does run_command() { echo “+” “$@”; “$@”; } means in bash script? 从命令窗口MATLAB运行脚本但不从运行图标运行时出错 - Errors running script from command window MATLAB but not from run icon 获取脚本后,sshpass 不会在 Ubuntu 远程主机上运行提供的命令,但会在 Windows 上运行,并且如果在同一脚本中运行 - sshpass will not run provided command on Ubuntu remote host when script is sourced, but does run on windows and if run within the same script Powershell脚本调用功能 - Powershell script call functions Powershell脚本无法识别功能 - Powershell script not recognising Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM