none
script is failing - no results RRS feed

  • Question

  • Hi All,

    i'm trying to use a script that I found here:

    https://gallery.technet.microsoft.com/scriptcenter/Add-NetworkLocation-734ab70a#content

    the instructions indicated that the script should return some results to indicate either success ($true) or failed ($false)

    I am getting no output at all. no errors, nothing printed to the screen when using -verbose.

    and it's also not creating the network location so I'm unsure where to begin troubleshooting.

    any help would be appreciated

    function Add-NetworkLocation
    <#
        Author: 
            
            Tom White, 2015.
        
        Description:
    
            Creates a network location shortcut using the specified path, name and target.
            Replicates the behaviour of the 'Add Network Location' wizard, creating a special folder as opposed to a simple shortcut.
    
            Returns $true on success and $false on failure.
    
            Use -Verbose for extended output.
    
        Example:
    
            Add-NetworkLocation -networkLocationPath "$env:APPDATA\Microsoft\Windows\Network Shortcuts" -networkLocationName "Network Location" -networkLocationTarget "\\server\share" -Verbose
    #>
    {
        [CmdLetBinding()]
        param
        (
            [Parameter(Mandatory=$true)][string]$networkLocationPath,
            [Parameter(Mandatory=$true)][string]$networkLocationName ,
            [Parameter(Mandatory=$true)][string]$networkLocationTarget
        )
        Begin
        {
            Write-Verbose -Message "Network location path: `"$networkLocationPath`"."
            Write-Verbose -Message "Network location name: `"$networkLocationName`"."
            Write-Verbose -Message "Network location target: `"$networkLocationTarget`"."
            Set-Variable -Name desktopIniContent -Option ReadOnly -value ([string]"[.ShellClassInfo]`r`nCLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}`r`nFlags=2")
        }
        Process
        {
            Write-Verbose -Message "Checking that `"$networkLocationPath`" is a valid directory..."
            if(Test-Path -Path $networkLocationPath -PathType Container)
            {
                try
                {
                    Write-Verbose -Message "Creating `"$networkLocationPath\$networkLocationName`"."
                    [void]$(New-Item -Path "$networkLocationPath\$networkLocationName" -ItemType Directory -ErrorAction Stop)
                    Write-Verbose -Message "Setting system attribute on `"$networkLocationPath\$networkLocationName`"."
                    Set-ItemProperty -Path "$networkLocationPath\$networkLocationName" -Name Attributes -Value ([System.IO.FileAttributes]::System) -ErrorAction Stop
                }
                catch [Exception]
                {
                    Write-Error -Message "Cannot create or set attributes on `"$networkLocationPath\$networkLocationName`". Check your access and/or permissions."
                    return $false
                }
            }
            else
            {
                Write-Error -Message "`"$networkLocationPath`" is not a valid directory path."
                return $false
            }
            try
            {
                Write-Verbose -Message "Creating `"$networkLocationPath\$networkLocationName\desktop.ini`"."
                [object]$desktopIni = New-Item -Path "$networkLocationPath\$networkLocationName\desktop.ini" -ItemType File
                Write-Verbose -Message "Writing to `"$($desktopIni.FullName)`"."
                Add-Content -Path $desktopIni.FullName -Value $desktopIniContent
            }
            catch [Exception]
            {
                Write-Error -Message "Error while creating or writing to `"$networkLocationPath\$networkLocationName\desktop.ini`". Check your access and/or permissions."
                return $false
            }
            try
            {
                $WshShell = New-Object -ComObject WScript.Shell
                Write-Verbose -Message "Creating shortcut to `"$networkLocationTarget`" at `"$networkLocationPath\$networkLocationName\target.lnk`"."
                $Shortcut = $WshShell.CreateShortcut("$networkLocationPath\$networkLocationName\target.lnk")
                $Shortcut.TargetPath = $networkLocationTarget
                $Shortcut.Description = "Created $(Get-Date -Format s) by $($MyInvocation.MyCommand)."
                $Shortcut.Save()
            }
            catch [Exception]
            {
                Write-Error -Message "Error while creating shortcut @ `"$networkLocationPath\$networkLocationName\target.lnk`". Check your access and permissions."
                return $false
            }
            return $true
        }
    }

    • Moved by Bill_Stewart Monday, December 17, 2018 6:36 PM This is not support forum for gallery scripts
    Monday, October 1, 2018 10:17 PM

All replies

  • All,

    I'm attempting to create a script that will duplicate the "add a network location" functionality.

    I'm very close, but one tiny thing bothers me about what I have:  it creates a shortcut and the icon created says "shortcut" underneath the name along with a file size:

    when I use the add network location wizard, it only displays the name, which is how I want it to look:

    When I look in the Network Shortcuts folder, I see that one is listed as a file folder with a target and one is listed as a shortcut:

    My script is creating a shortcut, clearly.  does anyone know how to use powershell to create the file folder with a target?

    param(
            [Parameter(Mandatory=$true)] [string]$Name,
            [Parameter(Mandatory=$true)] [string]$Target
        )
        $networkshortcut_name = $Name
        $networkshortcut_target = $Target
    
        $networkshortcuts_path = [Environment]::GetFolderPath('NetworkShortcuts')
    
        $networkshortcut_path = "$networkshortcuts_path"
    
        $targetlnk_path = "$networkshortcut_path\$networkshortcut_name.lnk"
    
        $WshShell = New-Object -com WScript.Shell
        $Shortcut = $WshShell.CreateShortcut($targetlnk_path)
        $Shortcut.TargetPath = $networkshortcut_target
        $Shortcut.Save()

    • Merged by Bill_Stewart Tuesday, October 2, 2018 2:49 PM Duplicate
    Monday, October 1, 2018 9:03 PM
  • I've done that, but at the bottom of the page it specifically refers me to the forums for assistance
    Monday, October 1, 2018 11:01 PM
  • update: I've edited the script above. it is now running and prompting for the correct input.

    It is also creating an icon, but when the icon is clicked it simply refers me right back to "This PC" rather than the target path.  

        [CmdLetBinding()]
        param
        (
            [Parameter(Mandatory=$true)][string]$networkLocationPath,
            [Parameter(Mandatory=$true)][string]$networkLocationName ,
            [Parameter(Mandatory=$true)][string]$networkLocationTarget
        )
        Begin
        {
            Write-Verbose -Message "Network location path: `"$networkLocationPath`"."
            Write-Verbose -Message "Network location name: `"$networkLocationName`"."
            Write-Verbose -Message "Network location target: `"$networkLocationTarget`"."
            Set-Variable -Name desktopIniContent -Option ReadOnly -value ([string]"[.ShellClassInfo]`r`nCLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}`r`nFlags=2")
        }
        Process
        {
            Write-Verbose -Message "Checking that `"$networkLocationPath`" is a valid directory..."
            if(Test-Path -Path $networkLocationPath -PathType Container)
            {
                try
                {
                    Write-Verbose -Message "Creating `"$networkLocationPath\$networkLocationName`"."
                    [void]$(New-Item -Path "$networkLocationPath\$networkLocationName" -ItemType Directory -ErrorAction Stop)
                    Write-Verbose -Message "Setting system attribute on `"$networkLocationPath\$networkLocationName`"."
                    Set-ItemProperty -Path "$networkLocationPath\$networkLocationName" -Name Attributes -Value ([System.IO.FileAttributes]::System) -ErrorAction Stop
                }
                catch [Exception]
                {
                    Write-Error -Message "Cannot create or set attributes on `"$networkLocationPath\$networkLocationName`". Check your access and/or permissions."
                    return $false
                }
            }
            else
            {
                Write-Error -Message "`"$networkLocationPath`" is not a valid directory path."
                return $false
            }
            try
            {
                Write-Verbose -Message "Creating `"$networkLocationPath\$networkLocationName\desktop.ini`"."
                [object]$desktopIni = New-Item -Path "$networkLocationPath\$networkLocationName\desktop.ini" -ItemType File
                Write-Verbose -Message "Writing to `"$($desktopIni.FullName)`"."
                Add-Content -Path $desktopIni.FullName -Value $desktopIniContent
            }
            catch [Exception]
            {
                Write-Error -Message "Error while creating or writing to `"$networkLocationPath\$networkLocationName\desktop.ini`". Check your access and/or permissions."
                return $false
            }
            try
            {
                $WshShell = New-Object -ComObject WScript.Shell
                Write-Verbose -Message "Creating shortcut to `"$networkLocationTarget`" at `"$networkLocationPath\$networkLocationName\target.lnk`"."
                $Shortcut = $WshShell.CreateShortcut("$networkLocationPath\$networkLocationName\target.lnk")
                $Shortcut.TargetPath = $networkLocationTarget
                $Shortcut.Description = "Created $(Get-Date -Format s) by $($MyInvocation.MyCommand)."
                $Shortcut.Save()
            }
            catch [Exception]
            {
                Write-Error -Message "Error while creating shortcut @ `"$networkLocationPath\$networkLocationName\target.lnk`". Check your access and permissions."
                return $false
            }
            return $true
        }
    

    Monday, October 1, 2018 11:03 PM
  • Gallery scripts are supported through the Gallery.  The statement you refer to does not say what you think it says.

    Read it again carefully:

    Online peer support     For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.

    It explicitly says to start a discussion on the discussions tab which is the link I posted.


    \_(ツ)_/

    Monday, October 1, 2018 11:30 PM
  • You just have to run the import-module on the file that you downloaded before you can execute the function that is defined within it. 

    What is the exact command that you are running that is referring you back to "My pc"? 

    I ran the sample and it created an obviously invalid link.

    Tuesday, October 2, 2018 12:15 AM
  • It is not a module.  Use dot sourcing to load the function into memory.

    Add the "-Verbose" parameter to the call to see the results.


    \_(ツ)_/

    Tuesday, October 2, 2018 12:47 AM
  • This Script Works fine for me:

     
    Add-NetworkLocation -networkLocationPath "$env:C:\Users\MyUser\Desktop\" -networkLocationName "NetLoc" -networkLocationTarget "\\MyServer\SharedFolder" -Verbose

    Tuesday, October 2, 2018 8:11 AM
  • jrv,

    I made a mistake trying to use dot sourcing. I see that it works now. But I found that import-module recognizes the code and dot sources it.

    -Dave 

    Tuesday, October 2, 2018 2:29 PM
  • Yes.  Both will work here.  Most references will suggest dot-sourcing.  Note that the opposite will not work.  You cannot dot-source a module.


    \_(ツ)_/

    Tuesday, October 2, 2018 3:09 PM
  • Maybe all you want/need is a symbolic link.

    mklink /d c:\Users\Dave\Documents\NetworkFiles  \\server\share

    C:\Users\Dave\Documents>mklink /?
    Creates a symbolic link.
    MKLINK [[/D] | [/H] | [/J]] Link Target
            /D      Creates a directory symbolic link.  Default is a file
                    symbolic link.
            /H      Creates a hard link instead of a symbolic link.
            /J      Creates a Directory Junction.
            Link    Specifies the new symbolic link name.
            Target  Specifies the path (relative or absolute) that the new link
                    refers to.
      
    Wednesday, October 3, 2018 5:35 PM