none
Powershell New-PSDrive - works ok but not if activated via a forms button? RRS feed

  • Question

  • Hi,  Hoping someone can advise as this issue is really bugging me.  I have a line of code that works perfectly, prompts for creds and maps drive in Powershell and explorer:

    New-PSDrive -Name t -PSProvider FileSystem -Root "\\pc\c$" -Credential pc\lapsadmin -Persist

    However I wanted to run the above via a button click.  I use a function and a form button

    $Button = New-Object System.Windows.Forms.Button$Button.Location = New-Object System.Drawing.Size(1200,30)
    $Button.Size = New-Object System.Drawing.Size(180,80)
    $Button.Text = "C$"
    $Button.Add_Click({procInfo})
    $Form.Controls.Add($Button

     Then a function with the exact line from above:

     

    function procInfo {

    New-PSDrive -Name t -PSProvider FileSystem -Root "\\pc\c$" -Credential pc\lapsadmin -Persist

    }

    and it doesn't work. It doesn't error just doesn't complete.  Please can someone advise what I'm doing wrong!

    thanks



    • Edited by Thumper71 Saturday, January 19, 2019 6:25 PM error
    Saturday, January 19, 2019 6:24 PM

Answers

  • Like this?

    $pc = 'Alpha'
    $form = New-Object System.Windows.Forms.Form
    $form.StartPosition = 'CenterScreen'
    $Button = New-Object System.Windows.Forms.Button
    $Button.Text = 'Connect C$ on $pc'
    $Button.Add_Click({
        Write-Host "Mapper to $pc" -ForegroundColor green
        Try{
            $X = New-PSDrive -Name X -PSProvider FileSystem -Root "\\$pc\c$" -Persist -Scope Global -ErrorAction Stop
            Write-Host 'Mapper end' -fore green
        }
        Catch{
            Write-Host "[Mapper Error]$_" -ForegroundColor red
        }
    })
    $Form.Controls.Add($Button)
    $form.ShowDialog()
    



    \_(ツ)_/

    • Marked as answer by Thumper71 Monday, January 21, 2019 9:19 PM
    Saturday, January 19, 2019 7:29 PM

All replies

  • Like this?

    $pc = 'Alpha'
    $form = New-Object System.Windows.Forms.Form
    $form.StartPosition = 'CenterScreen'
    $Button = New-Object System.Windows.Forms.Button
    $Button.Text = 'Connect C$ on $pc'
    $Button.Add_Click({
        Write-Host "Mapper to $pc" -ForegroundColor green
        Try{
            $X = New-PSDrive -Name X -PSProvider FileSystem -Root "\\$pc\c$" -Persist -Scope Global -ErrorAction Stop
            Write-Host 'Mapper end' -fore green
        }
        Catch{
            Write-Host "[Mapper Error]$_" -ForegroundColor red
        }
    })
    $Form.Controls.Add($Button)
    $form.ShowDialog()
    



    \_(ツ)_/

    • Marked as answer by Thumper71 Monday, January 21, 2019 9:19 PM
    Saturday, January 19, 2019 7:29 PM
  • Thanks Jrv - that works on my home machine, I'll test at work in the morning and confirm the result.

    Just out of curiosity would you say the "-scope global" is the key difference in making this work?

    *Update* checked on work machine and -scope global part made all the difference, thanks JRV, Marked as answer
    • Edited by Thumper71 Monday, January 21, 2019 9:20 PM update
    Sunday, January 20, 2019 11:19 AM