Get-CsUser error powershell - 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.

Related

Issue with executing Powershell commands

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

When using a function within a Powershell module, a psm1, is it possible to export the ExchangeOnline functions to be accessible in the console?

Working on several Powershell functions which connect to Office365 services and automate many of my usual tasks. Found when I have the function in a psm1, running the function to connect to ExchangeOnline the functions are not exposed to the console and only functions within the same module.
While I know I can export-modulemember, this only works for functions when the module is loaded, happens well before I connect-office365 -exchangeonline.
Is there anyway to export the commands loaded when connecting to Office 365 after connecting?
By placing the following function in a psm1 instead of a ps1 and loading it, and then running connect-off365 -exchangeonline, all Exchange Online related commands are not usable in the console. Yet, having the same function in a ps1 and loading it, the ExchangeOnline functions work.
Function Connect-Office365 {
Param(
[Switch]$ExchangeOnline,
[Switch]$EO
)
if ($EO) { $ExchangeOnline = $true }
elseif ($EO -and $ExchangeOnline) { Write-Warning "No need to declare ExchangeOnline and EO" }
$Credential = Get-Credential -message "Enter Office 365 credentials,`r`nor cancel to not connect to Office 365"
if ($null -eq $Credential) { Write-Host "Skipped entering credentials"; $TryAgain = $False }
if ($ExchangeOnline) {
Write-Host "Connecting to Exchange Online Services"
$global:ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -EA stop
Import-PSSession $global:ExchangeSession -AllowClobber -DisableNameChecking
}
}
For example
PS C:\Users\TechWithAHammer> import-module C:\scripts\Connect-Office365.psm1
PS C:\Users\TechWithAHammer> connect-office365 -eo Connecting to MSOL Services Connecting to Exchange Online Services
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0 tmp_3zvdxnnc.gd0 {Add-AvailabilityAddressSpace, Add-DistributionGroupMember...
PS C:\Users\TechWithAHammer> get-mailbox
get-mailbox : The term 'get-mailbox' 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-mailbox
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (get-mailbox:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
As per the answer in Commands from implicit remoting module not available when created from another module's function, found that I had to use Import-Module to import the Exchange Online and Compliance and Security center modules into the global session.
Import-Module (Import-PSSession $global:ExchangeSession -AllowClobber -DisableNameChecking) -Global

Install a pfx certificate on a remote server with powershell

By following this,
Install certificate with PowerShell on remote server
I'm trying to install a pfx certificate on a remote server by using the following Powershell command,
Invoke-command -ComputerName myservername -scriptblock { Import-PfxCertificate –FilePath D:\pfxcert.pfx cert:\localMachine\my -Password (ConvertTo-SecureString -String "mypassword" -Force –AsPlainText) }
This is giving below error message for me...
The term 'Import-PfxCertificate' 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.
+ CategoryInfo : ObjectNotFound: (Import-PfxCertificate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : myservername
Please help me out on this.
The Cmdlet Import-PfxCertificate is part of the module PKIClient.
The PKI Client Cmdlets in Windows PowerShell are only available on
Windows 8.1
Windows PowerShell 4.0
Windows Server 2012 R2
Try to load the PKI Client in your Script:
Invoke-command -ComputerName myservername -scriptblock
{
Get-Command -Module PKIClient;
Import-PfxCertificate –FilePath D:\pfxcert.pfx cert:\localMachine\my -Password (ConvertTo-SecureString -String "mypassword" -Force –AsPlainText)
}
You could try Get-Command -Module PKIClient to see all cmdlets.
The Import-PfxCertificate cmdlet does not exist on the target machine. Probably because it is running a version of Powershell less than 3.
You'll have to install a newer version of PowerShell if possible, or find a different method of importing the certificate.
Well, if you need to call it from C# anyway, then it will be probably worth to install it from C#, like
using System.Security.Cryptography.X509Certificates;
X509Certificate2 certificate = new X509Certificate2("C:\TEMP\yourcerthere.pfx", "yourpasswordhere");
X509Store store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();
It will be approx the same code in powershell actually, since it's no other way but use .net system class or some tool like this.

SCCM cmdlet from a remote PC

I'm trying to create a script to query members of a sccm2012 device collection so that I can pipe that into put into maintenance window. I need to run this from a remote machine.
Here's what I have - if I run each line by line it works fine.
But if I run it as a script continuously fails. If you could point me in the right direction please or solve for me. PS I'm not that good at PS but getting better.
PS F:\Install\Scritps> Enter-PSSession -ComputerName SERVER -ConfigurationName Microsoft.PowerShell32
Import-module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
cd P01:\
Import-module : The specified module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' was not loaded because no
valid module file was found in any module directory.
At line:4 char:1
+ Import-module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConso ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (C:\Program File...ionManager.psd1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
cd : Cannot find drive. A drive with the name 'P01' does not exist.
At line:6 char:1
+ cd P01:\
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (P01:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
I would guess that Enter-PSSession fails becauase it is designed for interactive use, and not to be used in a script. If that fails, then the following commands would fail because you're missing the SCCM 2012 Admin Console.
To run remote commands in a script, use Invoke-Command. Combine it with New-PSSession if you need a special configuration, different credentials etc.
$sess = New-PSSession -ComputerName SERVER -ConfigurationName Microsoft.PowerShell32
Invoke-Command -Session $sess -ScriptBlock {
Import-module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
cd P01:\
gci
}
Powershell Equivalent to Query in SCCM UI
I'm guessing you'd like to script the same queries as SCCM allows you to specify in this device collection query rules property dialog box:
The most reliable way to run such a query on an SCCM server from powershell on a workstation is using CIM. The trick is figuring out how to run the query so that it is interpreted in exactly the same way as if SCCM itself runs the query. Doing it this way seems to achieve that goal:
$SiteCode = 'ST1' # replace this with your SCCM site code
$SccmServerName = 'sccm01' # replace this with your server name
# replace this with your query
$query = #'
select
SMS_R_SYSTEM.ResourceID,
SMS_R_SYSTEM.ResourceType,
SMS_R_SYSTEM.Name,
SMS_R_SYSTEM.SMSUniqueIdentifier,
SMS_R_SYSTEM.ResourceDomainORWorkgroup,
SMS_R_SYSTEM.Client from SMS_R_System
inner join
SMS_G_System_COMPUTER_SYSTEM
on SMS_G_System_COMPUTER_SYSTEM.ResourceId = SMS_R_System.ResourceId
where
SMS_G_System_COMPUTER_SYSTEM.Manufacturer like "%Microsoft%"
'#
$params = #{
ComputerName = $SccmServerName
Query = $query
NameSpace = "Root\SMS\Site_$SiteCode"
}
Get-CimInstance #params
The query above happens to be for a device collection of Hyper-V guests . You can change $query to whatever you want. Once you get the query working here, you should be able to copy and paste it to/from the SCCM device colleciton UI.
On Powershell Remoting
If the call to Get-CimInstance results in an error that indicates that Powershell remoting or CIM is not working in your case, then you should probably focus on understanding that aspect of your environment. Powershell remoting has some nuanced limitations and prerequisites that depend, among other things, on firewall settings, enabling remoting, and what version of Powershell and operating systems you're running. It's not worth remembering all the details. The reference I use is Chapter 10 of Powershell in Depth, second edition.
If it turns out you must use WMI instead of CIM, it should be fairly straightforward to translate the call to Get-CimInstance to Get-WmiObject.
Frode F. you are the man
yep it works real good.
at the end after the } i put "Remove-PSSession -ComputerName SERVER"
so that when you run it a few times the concurrent session fill up. with this extra line no worries.

Running VMWare powerCLI commands with SoapUI

I have a powershell script file with PowerCLI commands like Connect-VIServer etc. I am able to run the script file in PowerGUI after I added the library references to VMWare PowerCLI but I don't know how to run it through SoapUI. I'm guessing it won't work with the regular Powershell CLI either. Is there any way to make this work? Here's the error, if it helps:
The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function
, script file, or operable program. Check the spelling of the name, or if a pat
h was included, verify that the path is correct and try again.
At Test.ps1:10 char:23
+ $vm = Connect-VIServer <<<< -Server $vcenterIP -User $vcenterUser -Password
$vcenterPW
+ CategoryInfo : ObjectNotFound: (Connect-VIServer:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
try calling this in the beginning of the script:
Add-PSSnapin "VMware.VimAutomation.Core" | Out-Null
You cannot execute PowerCLI commands outside of a Powershell host.
To add the snap-in to any Powershell host, use the command Tomas mentioned:
Add-PSSnapin VMware.VimAutomation.Core