Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Methods of PowerShell folder sharing and disk Mapping

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today Xiaobian to share with you about PowerShell folder sharing and disk mapping methods related knowledge, detailed content, clear logic, I believe that most people are too aware of this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.

PowerShell is a powerful scripting language and shell program framework, which is mainly used for Windows computers to facilitate system management and may replace the default command prompt on Windows in the future.

Overview of folder sharing

Shared folders are widely used, such as server-side file management, local area network file direct transfer and so on. Under linux, you can simply install smaba protocol and use it after simple configuration. Under windows, this feature can be turned on graphically, of course, with a bit of geek style, and we manage it through powershell (a small number of cmd).

Procedure to view the list of shares

Within powershell, we can get shared information by executing the following cmdlet:

λ Get-WmiObject-Class Win32_ShareName Path Description-----ADMIN$ C:\ Windows remote Administration C $C:\ default share D$ D:\ default share E $E:\ default share IPC$ remote IPCUsers C:\ Users

By the same token, under cmd, it is also possible

λ net share shared name resource note-C $C:\ Default share DD:\ default share E $E:\ default share IPC$ remote IPCADMIN$ C:\ Windows remote administration Users C:\ Users command completed successfully. Create a shared folder

Crazy Powershell

# share name $ShareName = 'TestShare'# share path $Path =' D:\ SHARE'If (! (Get-WmiObject-Class Win32_Share-Filter "name='$ShareName'")) {$Shares = [WMICLASS] "WIN32_Share" $Shares.Create ($Path,$ShareName,0) .ReturnValue} else {Write-Warning "$ShareName has been sharing!"}

If you have administrator privileges on the remote machine, also use WMI to create a new shared folder on the remote machine. Here is the code to create the shared folder on the remote host:

# ShareName = 'TestShare'# share path $Path =' D:\ SHARE'# remote hostname $Server = 'Server'If (! (Get-WmiObject-Class Win32_Share-Filter "name='$ShareName'")) {$Shares = [WMICLASS] "\\ $Server\ root\ cimv2:WIN32_Share" $Shares.Create ($Path,$ShareName 0) .ReturnValue} else {Write-Warning "$ShareName has been sharing!"} low-key cmd:: recommends viewing the current shared folder before creating net share TestShare=D:\ SHARE / users:25 / remark: "test share of the a folder"

We can easily turn on the shared state of a folder, and we can access it through the UNC path. After creating a file share, let's take a look at how to use it.

Drive mapping and shared access

Next, let's get rid of the graphical interface (if you don't like it, check it through your network neighbor ["Network"], or right-click on the computer icon to map the network drive). Let's use the command to enable it.

Tough Powershell

Temporarily create a network drive mapping:

(New-Object-ComObject WScript.Network) .MapNetworkDrive ("Z:", "\\ TEST-PC\ USERS")

Create a persistent network drive mapping:

# New-PSDrive plus the-Persist parameter makes the drive visible outside the PowerShell. # to actually create a permanent network drive, be sure to add-Scope Global. / # if New-PSDrive runs outside the global scope (for example, in a script), the drive will only appear in the file manager when the script is running. New-PSDrive-Name Z-PSProvider FileSystem-Root\\ TEST-PC\ USERS-Persist-Scope Global this command below the small cmd:: can be used in cmd to use this disk mapping, but cannot be loaded using Explorer. Net use Z:\\ TEST-PC\ USERS:: here is to map the systemroot folder to a z: drive, which can be loaded using "explorer.exe", but not the network path. Subst Z: $env:systemroot

After completing the above work, not surprisingly, your resource manager will show the icon of the network path you want to access.

Delete Shar

If you no longer need to use this shared folder, you can uninstall the network drive and delete the share on the shared host.

Powershell

$Shares = Get-WMIObject Win32_Share | Where {$_ .Name-eq ""} Foreach ($Share in $Shares) {$Share.Delete ()}

Cmd

Net share TestShare / delete Summary establishing shared folders requires enabling network sharing and discovery beforehand. Folder permission control and sharing permission control need to be done in advance. By configuring cmdkey, you can avoid authentication cmdkey / add:targetname / user:username / pass:password. These are all the contents of this article entitled "PowerShell folder sharing and disk Mapping". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report