1

Hoping this will be an easy one for somebody, but I just don't have the experience. I'm pulling the mapped network drive info from a PC. I'm testing the path to make sure it's active and that the person still has access. The test works fine. Here's my issue...

If the test is $true, I want it to output a line of code to a new .ps1 file for remapping the drive. I think I'm just about there, but I can't wrap my head around outputting a line of code and not the output of the line. Thanks for any help!

foreach ($Drive IN $Drives)
{
    $MND = $Network.OpenSubKey("$Drive")
    $Share = $MND.GetValue("RemotePath")
    $Path = Test-Path "$Share\*"
    IF ($Path -eq $true)
    {
        $ErrorActionPreference = "SilentlyContinue"
        Stop-Transcript | out-null
        $ErrorActionPreference = "Continue"
        Start-Transcript -path C:\output.ps1 -append
        "New-PSDrive -Name "$Drive" -PSProvider FileSystem -Root "$Share" -Persist"
        Stop-Transcript
    }
}
1
  • I'm an idiot. Instead of making it so hard on myself, I changed the whole IF to a single line "New-PSDrive -Name $Drive -PSProvider FileSystem -Root $Share -Persist" | Out-File C:\output.ps1 -Append Commented May 2, 2017 at 13:30

1 Answer 1

1

Per your comment, you probably don't want to be using Start-Transcript (as you'd get a lot of header content that you don't want), but rather something that outputs to a file like Out-File -Append or Add-Content.

You also need to remove the internal double quotes which are likely unnecessary (or you could replace them with single quotes):

"New-PSDrive -Name $Drive -PSProvider FileSystem -Root $Share -Persist" | Add-Content c:\output.ps1

It's worth also checking you have rights to write files to the root of C:\, this is blocked by default for non-admins in later versions of Windows.

Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate the tips! The root file was just an example. I'm writing to a folder on my desktop. I also discovered that I wanted to change it up a little more, because I'm doing this remotely and don't want to know if I have permissions. So I'm having it create the script with the whole Test-Path and such, as well. Just wanted to give a "Where Is He Now" update haha. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.