简体   繁体   中英

Reading a text file line by line

I'm new to power shell and can't seem to get this script correct.

$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'
$Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$users1\$users.lnk")
$Shortcut.TargetPath = "\\asrv1\users\$users"
$Shortcut.Save()

Every time it goes to "users.txt" it tries to read it as a complete file. Instead i would like for it to read as followed:

User1

User2

User3

I just can't seem to get this to work.

RE-EDIT:

What I'm trying to accomplish:

We went from one storage server to a new storage server.

ASRV1 to FSSRV

I want to create a shortcut from asrv1 to fssrv. Along with the upgrade we are also changing domains and changing everyone windows user names.

For example my user name was fMunoz and it was changed to fMunoz00.

I want to pull from a text file > users.txt with all the old usernames, and create shortcuts to the new user names storage file those users names are in a txt file called users1.txt.

If I understand your request, you want to create shortcuts in each home directory of the people in Users1.txt

This means you will need to complete multiple ForEach loops to cycle through both txt files.

$WshShell = New-Object -comObject WScript.Shell
$users = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users.txt'
$users1 = Get-Content '\\fssrv\homeshares$\fMunoz00\Desktop\users1.txt'

# For every line in $users1, Do something
Foreach($User in $users1) {
    # For every line in $users, Do something
    Foreach($Name in $users) {
        $Shortcut = $WshShell.CreateShortcut("\\Fssrv\homeshares$\$User\$Name.lnk")
        $Shortcut.TargetPath = "\\asrv1\users\$Name"
        $Shortcut.Save()
    }
}

I want to thank everyone for helping me.

I came up with a different solution for my problem. The way i fixed this issue was by creating an excel sheet w/ 2 columns.

OLD USERS | NEW USERS

I then created a mail merge using microsoft word. Then i continued with the following code in powershell to start my loop:

$WshShell = New-Object -comObject WScript.Shell

Once my loop was started with i then utilized mail merge to take <> from the excel sheet and replaced it with the insert it into my code. Also took <> and did the same.

$Shortcut = $WshShell.CreateShortcut("\\Fssrv\\homeshares$\\«New_Users»\\«Old_Users»_Z Drive.lnk") $Shortcut.TargetPath = "\\asrv1\\users\\«Old_Users»" $Shortcut.Save()

I hope this can help anyone else trying to accomplish something similar.

Yet again, Thanks & Good Luck...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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