如何通过 Powershell 或在 C# 中创建或查询多个 SMB 共享



我正在尝试创建数千个文件共享,如果它们尚不存在的话。

我必须这样做,因为我正在处理遗留系统。

在单个线程中同步执行此操作需要花费很长时间。我正在努力使其多线程,但如果可以的话,那就太好了:

  • 创造
  • 检查是否存在

具有 1 个 C# API/powershell 命令的多个 SMB 共享。我一直在尝试谷歌搜索答案,但没有找到任何有用的结果。:(

使用CIM cmdlet 需要 PS 版本 3.0 或更高版本,因此获取共享win32_share是最佳候选项,并且用于创建使用net share。你创建脚本在本地运行并将其包装在Invoke-Command中,-ComputerName接受计算机数组,它将处理批处理和线程。

Invoke-Command -ComputerName $Servers -ScriptBlock { #your command }

Invoke-Command -ComputerName $Servers -FilePath <your script path>

另请参阅-ThrottleLimit参数以及批处理。

Get-Help Invoke-Command -Full

仍有有关您正在创建的服务器和共享名的信息需要记录。它是否在所有计算机名上都使用相同的共享名?它是一个具有多个共享名的计算机名吗?这段代码可能会给你一些想法。这在Windows 7上运行,当前PowerShell 5.1。

#   Find CIM classes about shares.
#
#   Get-CimClass | Where-Object { $_.CimClassName -match '.*share.*' }
#
#   Find out which shares are already available.
#
#   Get-CimInstance -ClassName Win32_Share
#
#   Find class methods
#
#   Get-CimClass | Where-Object { $_.CimClassName -match '.*share.*' } | ForEach-Object { $_.CimClassMethods }
#
#   Find the arguments of a method.
#
#   (Get-CimClass -ClassName Win32_Share).CimClassMethods['Create'].Parameters
$Arguments = @{
Path = 'C:UsersPublic'
Name = 'TESTSHARE_TTTTTTTTTT';
Type = [uint32]0;
}
Invoke-CimMethod -ClassName Win32_Share -MethodName Create -Arguments $Arguments

最新更新