简体   繁体   English

创建像这样 powershell 脚本但在 VBS 中的网络位置

[英]Create Network Location like this powershell script but in VBS

I have this powershell function that creates network locations我有这个创建网络位置的 powershell function

function Add-NetworkLocation
{
    param(
        [string]$name,
        [string]$targetPath
    )
    
    # Get the basepath for network locations
    $shellApplication = New-Object -ComObject Shell.Application
    $nethoodPath = $shellApplication.Namespace(0x13).Self.Path

    # Créer l'emplacement réseau uniquement si le chemin local n'existe pas et que le chemin réseau existe
    if ((Test-Path $nethoodPath) -and !(Test-Path "$nethoodPath\$name") -and (Test-Path $targetPath))
    {
        # Create the folder
        $newLinkFolder = New-Item -Name $name -Path $nethoodPath -type directory

        # Create the ini file
        $desktopIniContent = @"
[.ShellClassInfo]
CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
Flags=2
ConfirmFileOp=1
"@
        $desktopIniContent | Out-File -FilePath "$nethoodPath\$name\Desktop.ini"

        # Créer le raccourci
        $shortcut = (New-Object -ComObject WScript.Shell).Createshortcut("$nethoodPath\$name\target.lnk")
        $shortcut.TargetPath = $targetPath
        $shortcut.IconLocation = "%SystemRoot%\system32\SHELL32.DLL, 85"
        $shortcut.Description = $targetPath
        $shortcut.WorkingDirectory = $targetPath
        $shortcut.Save()
        
        # Set attributes on the files & folders
        Set-ItemProperty "$nethoodPath\$name\Desktop.ini" -Name Attributes -Value ([IO.FileAttributes]::System -bxor [IO.FileAttributes]::Hidden)
        Set-ItemProperty "$nethoodPath\$name" -Name Attributes -Value ([IO.FileAttributes]::ReadOnly)
    } else {
        echo "Erreur : Un dossier avec ce nom existe deja"
    }
}

That creates real network location like this network location view这会创建像这个网络位置视图这样的真实网络位置网络位置视图that are automatically pinned in "This PC" and I want to do exactly the same thing with VBS.自动固定在“这台电脑”中,我想用 VBS 做同样的事情。

But I've tried to use this code:但我尝试使用此代码:

On Error Resume Next

Const NETHOOD = &H13&

Set objWSHShell = CreateObject("Wscript.Shell")

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path
strPath = strNetHood

DeleteFolder strPath

Set objWSHShell = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path
strPath = strNetHood

strPath = objFolderItem.Path & "\*.*"
On Error Resume Next
objFSO.DeleteFile strPath, true

strShortcutName = "Public"
strShortcutPath = "\\ads01\Public"

Set objShortcut = objWSHShell.CreateShortcut _
(strNetHood & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath = strShortcutPath
objShortcut.Save

But it doesn't create the same kind of network locations and they're not pinned neither on "This Pc" nor in "Quick Access"但它不会创建相同类型的网络位置,它们既没有固定在“这台电脑”上,也没有固定在“快速访问”中

Does anybody have an idea how to do the same thing in VBS?有人知道如何在 VBS 中做同样的事情吗?

Your example worked ok for me?你的例子对我有用吗? You might want to remove all your instances of 'On Error Resume Next' to see if something is throwing an error, or maybe an issue accessing '\ads01' etc.您可能希望删除所有“On Error Resume Next”实例,以查看是否有问题引发错误,或者访问“\ads01”等是否存在问题。

Option Explicit

Const NETHOOD = &H13&

Dim objWshShell  : Set objWshShell = CreateObject("Wscript.Shell")
Dim objShell  : Set objShell = CreateObject("Shell.Application")
Dim strNetHood : strNetHood = objShell.Namespace(NETHOOD).Self.Path

Dim strShortcutName  : strShortcutName = "Example"
Dim strShortcutPath  : strShortcutPath = "C:\temp"

Dim objShortcut : Set objShortcut = objWshShell.CreateShortcut(strNetHood & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath = strShortcutPath
objShortcut.Save

Set objShortcut = Nothing
Set objWshShell = Nothing
Set objShell = Nothing

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM