SOLVED

Using multiple variables against single command

%3CLINGO-SUB%20id%3D%22lingo-sub-1435916%22%20slang%3D%22en-US%22%3EUsing%20multiple%20variables%20against%20single%20command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1435916%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20working%20with%20a%20loop%20in%20a%20script%20and%20am%20trying%20to%20get%20multiple%20variables%20for%20use%20in%20a%20single%20command.%20This%20is%20a%20hard%20one%20to%20verbalize%20for%20a%20Google%20search%20so%20I%20figured%20I%20would%20try%20here.%20The%20script%20I%20am%20running%20is%20this....%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24username%20%3D%20get-content%20c%3A%5Ctemp%5Cusers.txt%0A%24newadmin%20%3D%20Read-Host%20'Enter%20New%20Admin'%0A%24OneDriveUrl%20%3D%20Get-PnPUserProfileProperty%20-Account%20%24username%20%7C%20select-object%20PersonalUrl%0Aforeach%20(%24user%20in%20%24username)%7B%0ASet-SPOUser%20-Site%20%24OneDriveUrl.PersonalUrl%20-LoginName%20%24newadmin%20-IsSiteCollectionAdmin%20%24True%20-ErrorAction%20SilentlyContinue%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EWhat%20I%20am%20trying%20to%20accomplish%20is%20to%20take%20the%20info%20gathered%20in%20the%20%3CSTRONG%3E%24username%3C%2FSTRONG%3E%2C%20%3CSTRONG%3E%24newadmin%3C%2FSTRONG%3E%20and%20%3CSTRONG%3E%24OneDriveUrl%3C%2FSTRONG%3E%20variables%20and%20loop%20them%20against%20the%20%3CSTRONG%3ESet-SPOUser%3C%2FSTRONG%3E%20command%20in%20the%20last%20line%20of%20the%20script.%20The%20script%20works%20fine%20without%20the%20foreach%20loop%20for%20doing%20user%20by%20user%20individually%2C%20but%20I%20have%20a%20large%20list%20of%20users%20who%20I%20want%20to%20work%20with.%20Would%20love%20some%20help%20on%20how%20I%20can%20gather%20all%20the%20info%20from%20the%20variables%20and%20run%20them%20against%20the%20final%20Set-SPOUser%20command%20at%20the%20end.%20I%20appreciate%20the%20help%20and%20thanks%20in%20advance!!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1435916%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1436812%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20multiple%20variables%20against%20single%20command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1436812%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20script%20looks%20OK%2C%20although%20you%20need%20to%20move%20the%20%24OneDriveUrl%20part%20inside%20the%20loop%20(otherwise%20it%20will%20always%20return%20the%20same%20value).%26nbsp%3B%20I%20would%20suggest%20generating%20a%20list%20of%20individual%20ODFB%20URLs%20first%2C%20then%20combining%20it%20with%20the%20username%20in%20a%20CSV%20file.%20That%20way%20you%20can%20simply%20reference%20the%20different%20values%20via%20say%20%24_.UserName%2C%20%24_.OneDriveUrl%20and%20so%20on.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1437732%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20multiple%20variables%20against%20single%20command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1437732%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20response%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3BI%20tried%20moving%20the%20%24OneDriveUrl%20portion%20of%20the%20script%20inside%20the%20loop%20and%20no%20dice.%20Although%20I%20may%20have%20it%20wrong.%20I%20have%20to%20admit%20I%20am%20not%20a%20Powershell%20wiz%20by%20any%20means.%20I%20have%20change%20the%20script%20like%20below...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24username%20%3D%20get-content%20c%3A%5Ctemp%5Cusers.txt%0A%24newadmin%20%3D%20Read-Host%20'Enter%20New%20Admin'%0Aforeach%20(%24user%20in%20%24username)%7B%0A%24OneDriveUrl%20%3D%20Get-PnPUserProfileProperty%20-Account%20%24username%20%7C%20select-object%20PersonalUrl%0ASet-SPOUser%20-Site%20%24OneDriveUrl.PersonalUrl%20-LoginName%20%24newadmin%20-IsSiteCollectionAdmin%20%24True%20-ErrorAction%20SilentlyContinue%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1439481%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20multiple%20variables%20against%20single%20command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1439481%22%20slang%3D%22en-US%22%3E%3CP%3EInside%20the%20loop%2C%20you%20should%20be%20using%20%24user%20instead%20of%20%24username.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1440166%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20multiple%20variables%20against%20single%20command%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1440166%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3Bthat%20was%20my%20issue.%20I%20confirmed%20its%20working%20now.%20Thank%20you%20very%20much%20for%20you%20help!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

I am working with a loop in a script and am trying to get multiple variables for use in a single command. This is a hard one to verbalize for a Google search so I figured I would try here. The script I am running is this....

 

$username = get-content c:\temp\users.txt
$newadmin = Read-Host 'Enter New Admin'
$OneDriveUrl = Get-PnPUserProfileProperty -Account $username | select-object PersonalUrl
foreach ($user in $username){
Set-SPOUser -Site $OneDriveUrl.PersonalUrl -LoginName $newadmin -IsSiteCollectionAdmin $True -ErrorAction SilentlyContinue}

What I am trying to accomplish is to take the info gathered in the $username, $newadmin and $OneDriveUrl variables and loop them against the Set-SPOUser command in the last line of the script. The script works fine without the foreach loop for doing user by user individually, but I have a large list of users who I want to work with. Would love some help on how I can gather all the info from the variables and run them against the final Set-SPOUser command at the end. I appreciate the help and thanks in advance!!

4 Replies
Highlighted
Best Response confirmed by charlie4872 (Contributor)
Solution

The script looks OK, although you need to move the $OneDriveUrl part inside the loop (otherwise it will always return the same value).  I would suggest generating a list of individual ODFB URLs first, then combining it with the username in a CSV file. That way you can simply reference the different values via say $_.UserName, $_.OneDriveUrl and so on.

Highlighted

Thanks for the response @Vasil Michev I tried moving the $OneDriveUrl portion of the script inside the loop and no dice. Although I may have it wrong. I have to admit I am not a Powershell wiz by any means. I have change the script like below...

 

$username = get-content c:\temp\users.txt
$newadmin = Read-Host 'Enter New Admin'
foreach ($user in $username){
$OneDriveUrl = Get-PnPUserProfileProperty -Account $username | select-object PersonalUrl
Set-SPOUser -Site $OneDriveUrl.PersonalUrl -LoginName $newadmin -IsSiteCollectionAdmin $True -ErrorAction SilentlyContinue}
Highlighted

Inside the loop, you should be using $user instead of $username.

Highlighted

Thanks @Vasil Michev that was my issue. I confirmed its working now. Thank you very much for you help!