Issue with executing Powershell commands - powershell

I'm trying to execute the following commands in Microsoft Powershell console (with Administrator right):
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Import-Module MicrosoftTeams
$userCredential = Get-Credential
$sfbSession = New-CsOnlineSession -Credential $userCredential
Import-PSSession $sfbSession
The first three commands are executed without any issue but when I try to execute $sfbSession = New-CsOnlineSession -Credential $userCredential I'm getting the following error:
New-CsOnlineSession : The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:15
+ $sfbSession = New-CsOnlineSession -Credential $userCredential
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-CsOnlineSession:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have tried to look on various online forums but it's not solving my issue. On Microsoft's website they say that we have to use MicrosoftTeams module instead of SkypeOnlineConnector module but it's not working in my case. I tried various steps but all in vain. I hope someone got an idea how to solve this issue.
Thanks.
Regards,
Seeya

Related

Running Powershell script using inline admin credentials to start and stop windows services

I have access as an admin on a machine and I have to Start, Stop and Restart some of the window services using the Powershell script. We don't want to have UAC prompt while running the script because only one user would have access to that machine. Also due to some specific requirements, we have to have run that script file by adding the admin credentials inside it.
Along with other solutions I have tried so far, the one close to what I am looking for is as follows
$username = "Domain\user"
$password = ConvertTo-SecureString "myPassword" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Set-Service -Name Spooler -Status Running -PassThru -Credential $psCred
But I am getting following error.
Set-Service : A parameter cannot be found that matches parameter name
'Credential'. At line:6 char:53
+ ... t-Service -Name Spooler -Status Running -PassThru -Credential $psCred
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Service], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetServiceCommand
Update 1 (Trying suggestion)
Invoke-Command -credential $psCred -command {Set-Service -Name Spooler -Status Running -PassThru} -computername myComputerName
[myComputerName] Connecting to remote server myComputerName failed
with the following error message : The client cannot connect to the
destination specified in the request. Verify that the service on the
destination is running and is accepting requests. Consult the logs and
documentation for the WS-Management service running on the
destination, most commonly IIS or WinRM. If the destination is the
WinRM service, run the following command on the destination to analyze
and configure the WinRM service: "winrm quickconfig". For more
information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (myComputerName:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken
Update 2
I had to enabled PSRemoting on machine using command Enable-PSRemoting -Force -SkipNetworkProfileCheck so Invoke-Command can execute but after repeating the command Invoke-Command -credential $psCred -command {Set-Service -Name Spooler -Status Running -PassThru} -computername localhost I got following error
[localhost] Connecting to remote server localhost failed with the
following error message : Access is denied. For more information, see
the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
Running the same set of command in elevated powershell window is working fine.
Can you please guide me in the right direction considering I am newbie in the Powershell world.
As mentioned in the official documentation of Set-Service
Can you try like this?
$Cred = Get-Credential
$hostname = "$env:computername.$env:userdnsdomain"
Write-Host $hostname
Invoke-Command -ComputerName $hostname -Credential $Cred -ScriptBlock{ Start-Service -Name Spooler}
Also if you don't want to prompt for the credentials then you can store the credentials in the Windows credentials manager and get it from their in your PowerShell script. Refer to this answer for using credential manager with PowerShell

Use Connect-SPOService with Powershell 6 (core version)

I'm trying to connect to a sharepoint environment and I want to do that with Powershell version 6. Why? Eventually, I want to put the PS commands in a .net core 3 application. And as far as I know I cannot use PS5.1 in .net core.
It is about this powershell script:
Import-Module -Force -name Microsoft.Online.SharePoint.PowerShell;
Import-Module -Force -name Microsoft.Online.SharePoint.PowerShell -DisableNameChecking;
$username = 'admin#shootme.com';
$password = 'right now';
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force);
Connect-SPOService -Url https://shootme.sharepoint.com -Credential $cred;
When I try this in the default PS 5.1 it just works fine. When I try this with PS 6.2.3, I get an error:
Connect-SPOService : The remote server returned an error: (400) Bad Request.
At line:1 char:1
+ Connect-SPOService -Url https://shootme.sharepoint.com -Credent ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-SPOService], WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService
Does the newer Powershell have different syntax orso, of what am I doing wrong?
Also, maybe there is a way to run scripts in ps 5.1 when running them in .net core?
Have you tried connecting manually by removing the credentials portion and letting it prompt you for a login and test if that resolves successfully?
Edit: I do know you can also call powershell from a .bat like so:
powershell -version 2 .\xyz.ps1
But not knowing what you're going for exactly makes it tough to suggest if that's even a viable option.

New-PSSession :Connecting to remote server outlook.office365.com failed :The parameter is incorrect

I am trying to run the following command on a server(running on Windows Server 2012 R2) in order to import the exchange online PowerShell(I am using the PowerShell ISE with version 5 of PowerShell) commands such as "Search-UnifiedAuditLog" but whenever I run the command I receive an error. My code works while on my local machine(Windows 7 with PowerShell V4) but when I remote connect onto the server to try and run the command it doesn't work.
I am aiming to have a script including this command run as a scheduled task which is why it needs run on the server. I've tried to run this command on several user accounts on the server and still get the same issue. Running the command using the same user accounts as before but using my local machine does not result in an error.
I have tried to set the execution policy to RemoteSigned using the following command but this has had no effect.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
My current code looks like;
$ExchangeCredentials = Get-StoredCredential -Target Credentials
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $ExchangeCredentials -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking -AllowClobber
The full error I receive is;
New-PSSession : [outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : The parameter is incorrect. For more information, see the
about_Remote_Troubleshooting Help topic.
At line:1 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : 87,PSSessionOpenFailed
I'd expect this command to run fine like on my local machine. I've not been able to find anyone else with this same issue.
Based on the error message, New-PSSession is causing the issue. As all the other parameters look correct, I would check what you are passing to the Credential parameter.
Does Get-StoredCredential -Target Credentials obtain a correct PSCredential object?

Import-module: Could not find a part of the path

I am trying to connect to Exchange Online using PowerShell (As per this documentation) to add distribution group members.
However I am getting the following error:
Import-Module : Could not find a part of the path
'C:\Users\[my user account]\AppData\Local\Temp\tmp_hhw3s30w.xwu\tmp_hhw3s30w.xwu.format.ps1xml'.
At line:3 char:17
+ ... Import-Module -Name $name -Alias * -Function * -Prefix $p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:String) [Import-Module], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand
My code is as follows:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $adCreds -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking -AllowClobber
Connect-MsolService -Credential $adCreds
My code has worked in the past on my machine, so I am struggling to work out why it is failing now.
Any help would be massively appreciated.
Another thing to try is closing and reopening PowerShell.
For me, I had deleted everything in my local temp folder earlier in the session. It seems PowerShell was using some of what was deleted.
When I reopened PowerShell, it apparently recreated the temp files/directories it needed, and Import-Module worked again.
I know this is an old thread but I had the same issue today. The way I fixed it was to update the module that imports modules, PowerShellGet
To check your current module version, enter: Get-Module -Name PowerShellGet
If you're still on version 1.x.x.x, update it with: Install-Module -Name PowerShellGet -Force -AllowClobber
You may need to close and reopen PowerShell. Be sure to check the version again and compare it with what's available at https://www.powershellgallery.com/packages/PowerShellGet/2.2.5

Get-CsUser error powershell

I am running this command in powershell Get-CsUser and i am getting the following error
Get-CsUser : The term 'Get-CsUser' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is
correct and try again. At line:1 char:1
+ Get-CsUser
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-CsUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
What do I have to do to avoid this error? I have imported:
Import-Module "C:\Program Files\Common Files\Skype for Business Online\Modules\SkypeOnlineConnector\SkypeOnlineConnector.psd1"
and then made connection to Office 365 Skype for business service with the following script:
$credential = Get-Credential
Import-Module MSOnline
Connect-MsolService -Credential $credential
Import-Module SkypeOnlineConnector
$lyncSession = New-CsOnlineSession -Credential $credential
Import-PSSession $lyncSession
Does anyone knows what's the cause of this error?
Does anyone knows what s the cause of this error? thank you in advance
Problem is office 365 skype for business does not have a command "Get-CsUser" under User cmdlets.
Available User cmdlets are as follows.
Set-CsUser
Get-CsUserPstnSettings
Set-CsUserPstnSettings
Get-CsUserSession
Source "Skype for Business Online cmdlets"
Get-CsUser is available for on-premises lync servers.
.
PS:
Your connection for "office 365 skype for business" and the session import should be ok, for example if you type Get-CsUserSession, it should not say "The term 'Get-CsUserSession' is not recognized as the name of a cmdlet, function, script file, or operable program"
(1) You can view available commands in your imported module using below
Get-Command -Module MSOnline
Get-Command -Module SkypeOnlineConnector
(2) You can also view available commands in your current session by using Get-Command and your temporary session name.
Get-Command -Module tmp_aqu5qmxt.frb
tmp_aqu5qmxt.frb is my temperory session id name .. change it to whatever you get at this screen.
(3) To view all imported commands from all your sources, (modules+sessions) you can use,
Get-Command -ListImported
None of them has a command called "Get-CsUser"
In Skype for Business Online you should use Get-CsOnlineUser instead of Get-CsUser.
As stated in the documentation os Get-CsUser it's not available for Skype for Business Online.
If you use Get-Command Get-Cs*User you'll find only the following 3 commands:
Get-CsOnlineDialInConferencingUser
Get-CsOnlineUser
Get-CsOnlineVoiceUser
The second one is the one you should use to display users homed online. I already requested a change to MS documentation to include that as a hint, hopefully they you'll accept that.