How to launch teams.exe with powershell - powershell

I've been trying to launch teams using powershell, i've succeeded launching teams using the destination path under the shortcut property, and then running it in powershell.
C:\Users\user1\AppData\Local\Microsoft\Teams\Update.exe --processStart "Teams.exe"
But this only works for the local user, if i run the script with another user teams won't launch. i've storing the user in a variable.
$user = $env:UserName
C:\Users\$user\AppData\Local\Microsoft\Teams\Update.exe --processStart "Teams.exe"
This doesn't work.
So my question how can I launch teams.exe (make it visible)?
I want to make it like how you can launch word with start winword.exe

You should get the variable for the App Data folder, not just the username. Otherwise, it won't work in situations where the user's profile folder is in a different location:
$user = "$($env:LOCALAPPDATA)\Microsoft\Teams\Update.exe --processStart `"Teams.exe`""

"%LOCALAPPDATA%\Microsoft\Teams\Update.exe" --processStart "Teams.exe"

This can be fixed by using the following format instead:
$user = $env:UserName
'C:\Users\'+$user+'\AppData\Local\Microsoft\Teams\Update.exe --processStart "Teams.exe"'

Related

Having trouble setting Remote Desktop Services Control permissions?

I am new to power shell and I am trying to create a script that would create new users in Active Directory. Currently I am having trouble setting the Remote Desktop Services tab of the User. My code is below.
#Set Remote Control Settings Permissions
#I recieved Server is not operational error. https://learn.microsoft.com/en-us/troubleshoot/windows-server/identity/fail-to-configure-server-using-server-manager I think Invoke Set may not be the command I should use.
$userpath = dsquery user -samid $username
$userpat = "LDAP://$userpath"
$userp = [ADSI]$userpat
$userp.InvokeSet("EnableRemoteControl",2)
$userp.setinfo()
#Remote Desktop Services User profile profile path set to "\\documentsf\profiles\$username" THIS IS MESSING UP ERROR OCCURRS WITH INVOKE SET SAYING NOT SPECIFIED
$userp.InvokeSet("terminalservicesprofilepath","\\documentsf\profiles\$username")
$userp.setinfo()
The remote control permissions are a little strange and work best with the ADSI method, which you're close to already. dsquery actually returns a string with quotes inside it, so you'll either need to strip those quotes first or use a different method - I prefer Get-ADUser:
$LdapUser = "LDAP://" + (Get-ADUser $username).distinguishedName
$User = [ADSI]$LdapUser
$User.InvokeSet("EnableRemoteControl",2)
$User.setinfo()
And to set the remote desktop services profile path for a user:
$User.invokeset("terminalservicesprofilepath","\\Server\Share\$username")
$User.SetInfo()

Executing Scripts is different when running under different user than the logged in user

I have two different users, my regular user and my admin user. Both have profiles setup for PowerShell. I log into my workstation ONLY as my regular user. My issue is that when running PowerShell as my regular user, I can type "menu" at the prompt from any folder and it will run the Menu.ps1 script from the scripts folder. When I try running PowerShell as my admin user, I get "The Term 'menu' is not a blah blah blah". The only way I can run it is if I change the the PSDrive named scripts: and dot source execute from there.
The only difference that I can find is that my regular user has access to a windows mapped drive z: (its in the Path environment variable also), while my admin user does not. I was hoping that I would just need to add scripts:
to the environment variable but that didn't help either.
Any assistance is appreciated.
Thank You Jeff Zeitlin.
I added this to my Admin users profile, works perfectly.
$ScriptsPath = "\\Server\Share\Scripts"
$ScriptsDrive = "Z:"
$Network = New-Object -ComObject "Wscript.Network"
$Network.MapNetworkDrive("$($ScriptsDrive)","$($ScriptsPath)")
$strPath=$env:path
if (!($strPath.ToUpper().Contains($ScriptsDrive))) {
$env:path += ";" + $ScriptsDrive + "\"
}

How to get an environment variable in a Powershell script when it is deployed by SCCM?

I've made a script to automatically change and/or create the default Outlook signature of all the employees in my company.
Technically, it gets the environment variable username where the script is deployed, access to the staff database to get some information regarding this user, then create the 3 different files for the signature by replacing values inside linked docx templates. Quite easy and logical.
After different tests, it is working correctly when you launch the script directly on a computer, either by using Powershell ISE, directly by the CMD or in Visual Studio. But when we tried to deploy it, like it will be, by using SCCM, it can't get any environment variable.
Do any of you have an idea about how to get environment variables in a script when it is deployed by SCCM ?
Here is what I've already tried :
$Name = [Environment]::UserName
$EnvVarUserName = Get-Item Env:\USERNAME
Even stuff like this :
$proc = gwmi win32_process -Filter "Name = 'explorer.exe'"
$report = #()
ForEach ($p in $proc)
{
$temp = "" | Select User
$temp.user = ($p.GetOwner()).User
$report += $temp
}
Thanks in advance and have a nice day y'all !
[EDIT]:
I've found a way of doing this, not the best one, but it works. I get the name of the machine, check the DB where when a laptop is connected to our network it stores the user id and the machine, then get the info in the staff DB.
I will still check for Matt's idea which is pretty interesting and, in a way, more accurate.
Thank you all !
How are you calling the environmental variable? $Env:computernamehas worked for me in scripts pushed out via SCCM before.
Why don't you enumerate the "%SystemDrive%\Users" folder, exclude certain built-in accounts, and handle them all in one batch?
To use the UserName environment variable the script would have to run as the logged-in user, which also implies that all of your users have at least read access to your staff database, which, at least in our environment, would be a big no-no.

Powershell to find machine that created a file

I have a script that monitors the filesystem using FileWatcher.IO in Powershell.
Currently it finds the user that made the file with:
$owner = (Get-Acl $path).Owner
And it finds the computer that the file was made on with:
$Computer = get-content env:computername
But I'd also like to obtain what machine the file was created from. For instance, if a user is logged into a terminal server, I can see the file is made on the terminal server. But I want to know the host name of the local machine that made the file on the terminal server.
Is this possible? I've been searching the msdn PSEventArgs Class page without much success.
That information is not going to be stored in the file or its metadata, so no there's no straightforward way to get at it.
By the way, you can just use $env:computername directly as a variable; there's no need to use Get-Content.

Get currently logged in users with powershell to add shortcut to desktop

I have a script that I deploy using LANDesk and in the script it calls a powershell script to add a shortcut to a network folder on the user's desktop. LANDesk logs in to run the script with a local machine account but I don't want the shortcut to be put on that desktop but of the desktop of the user that is currently logged in. Is there a way to do this in powershell?
Here is my code for adding the shortcut if you run the script as the logged in user (can't run as the user themselves because they don't have admin rights)
$wshshell = new-object -comobject WScript.Shell
$Ink = $wshshell.CreateShortcut("$home\Desktop\PI_Users.lnk")
$Ink.TargetPath = "\\htntfs04\PI_Users"
$Ink.Save()
Thanks in advance for help.
If you're using LANDesk, and you're using a package to distribute the script, you can choose between executing the script as LocalSystem which is the default, or as the Current user's account.
Just open the properties of the package, go to Accounts and choose "Current user's account". The task will fail if there's no user logged in to the machine.
But, if I understand correctly, the problem is that the user is not allowed to create a link on its own desktop? If so, this solution won't work and the task would fail anyway!
Another approach I often use would be to execute a script that loops through all the local profiles and creates a link on the desktop of each user. If it's okay for you to use a WSH script instead of a powershell script, you could use something like this:
Const HKEY_LOCAL_MACHINE = &H80000002
Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
Set ws = CreateObject("Wscript.Shell")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
For Each objSubkey In arrSubkeys
strValueName = "ProfileImagePath"
strSubPath = strKeyPath & "\" & objSubkey
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE, strSubPath, strValueName, strProfile
If Left(strProfile, Len(ws.ExpandEnvironmentStrings("%windir%"))) <> ws.ExpandEnvironmentStrings("%windir%") Then
Set objShtCut = ws.CreateShortcut(strProfile & "\Desktop\PI_Users.lnk")
objShtCut.TargetPath = "\\htntfs04\PI_Users"
objShtCut.Save
Set objShtCut = Nothing
End If
Next
Can't you use [Environment]::GetFolderPath("Desktop") to get the desktop's path for current user?