This is what I've got in my Invoke-ProcessTable powershell script (snippet)
# create pscredential object
$credObject = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $ServicePrincipal, $secStringPassword
#Get App Id for Service Principle
$SPDetails = Get-AzADServicePrincipal -DisplayName $ServicePrincipal
$SP_AppId = ($SPDetails).AppId
Invoke-ProcessTable `
-TableName $TableName `
-DatabaseName $DatabaseName `
-RefreshType Full `
-Server $Server `
-ServicePrincipal `
-Credential $credObject `
-TenantId $TenantId `
-ApplicationId $SP_AppId
$TenantId is hard coded in the script.
and this is the error I'm getting
Invoke-ProcessTable : The provided application id 'app:"service
principle name"#6"tenant id"5' is invalid. Parameter name: id At
C:\temp\powerrshell\invoke-processtable.ps1:25 char:1
Invoke-ProcessTable `
+ CategoryInfo : NotSpecified: (:) [Invoke-ProcessTable], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ProcessTable
I wasn't expecting the Application Id to be in the format that is being shown in the error message.
Anyway, where am I going wrong?
School boy error
I was passing in the wrong parameter to the $credObject object
I was sending in the ServicePrincipleName ($ServicePrincipal) instead of the Application Id ($SP_AppId).
Once I did this all worked.
#Get App Id for Service Principle
$SPDetails = Get-AzADServicePrincipal -DisplayName $ServicePrincipal
$SP_AppId = ($SPDetails).AppId
# create pscredential object
$credObject = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $SP_AppId, $secStringPassword
Related
I'm new to PowerShell. I am trying to make it so I can setup a new computer connecting to the network to allow me to do certain tasks. When I run this:
$domain = "mydomain.com"
$mycred = get-credential
$credential = New-Object System.Management.Automation.PSCredential("$($domain)\$($mycred.Username)","$($mycred.Password)")
$compName = Read-Host -Prompt "Enter new computer name"
Add-Computer -DomainName $domain -newname $compName -Credential $credential -Restart
Pause
I get the error:
New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
At C:\Users\entername\Downloads\1-JoinDomainCred.ps1:7 char:15
... redential = New-Object System.Management.Automation.PSCredential("$($ ...
CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Where am I going wrong?
Get-Credential aready returns a proper credentials object. Just use that:
$mycred = Get-Credential; Add-Computer ... -Credential $mycred
PowerShell is not C#, pass the arguments as an array without the ():
$credential = New-Object System.Management.Automation.PSCredential "$($domain)\$($mycred.Username)",$mycred.Password
EDIT:
I want to retrieve session data from a specific account using
PowerShell. According to this documentation:
https://learn.microsoft.com/en-us/powershell/module/skype/get-csusersession?view=skype-ps
Get-CsUserSession command is able to do this. I am using this
command according to the upper's link example
Get-CsUserSession -User account#companyX.onmicrosoft.com -StartDate "6/1/2018 07:00 PM"
and then I am getting the following error:
A parameter cannot be found that matches parameter name 'StartDate'.
+ CategoryInfo : InvalidArgument: (:) [Get-CsUserSession], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Rtc.Management.Hosted.Data.GetCsUserSessionCmdlet
+ PSComputerName : admin1e.online.lync.com
What is wrong with that and what is the correct declaration?
I am making a connection to 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
What I would like to do is to set using a particular static account and password from the PowerShell script (using some sort of declaration variable strings), instead of running this command and have to type the credentials in a separate window. Meaning that I want to avoid using $credential = Get-Credential command. Is this possible?
As stated in documentation you linked (only at the top paragraph though), you have to use StartTime not StartDate. The error you receive is the typical symptom that you either has a typo in parameter name or this parameter doesn't exist for that function.
I'll request to change the example in the docs a bit later, seems like someone who wrote them were mixing up with another cmdlet.
Edit: to store credentials you can export your password like this:
"P#ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File C:\Users\username\password2.txt
And then import like this:
$password = Get-Content -Path "C:\Users\USUARIOPC\password2.txt" | ConvertTo-SecureString -String $password
$credential = New-Object System.Management.Automation.PsCredential("yourlogin#domain.com", $password)
In the meantime, I tried the following query. Probably is not too safe to use a password in a script but for us who want to do it like this is a nice solution.
$username = "account1#companyX.onmicrosoft.com"
$password = "abcdefg"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$credential = $cred
Import-Module MSOnline
Connect-MsolService -Credential $credential
Import-Module SkypeOnlineConnector
$SFBSession = New-CsOnlineSession -Credential $credential
Import-PSSession $SFBSession
I'm currently trying to create an automation runbook to process a cube in Azure. I've tried multiple PowerShell scripts like this one :
$AzureCred = Get-AutomationPSCredential -Name "RefreshTest"
Add-AzureRmAccount -Credential $AzureCred | Out-Null
Invoke-ProcessASDatabase -databasename "MKTGCube" -server "AzureServerName" -RefreshType "Full" -Credential $AzureCred
With that kind of error (despite the fact that I installed the SQLServer module).
Invoke-ProcessASDatabase : The term 'Invoke-ProcessASDatabase' 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.
Or this script :
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
##Getting the credential which we stored earlier.
$cred = Get-AutomationPSCredential -Name 'CredMat'
## Providing the Server Details
$ServerName = "AzureServerName"
Invoke-ProcessASDatabase -databasename "MKTGCube" -server $ServerName –ProcessType "ProcessFull"
$error[0].Exception.Message
$error[0].Exception.StackTrace
With that error message.
Invoke-ProcessASDatabase : Authentication failed: User ID and Password
are required when user interface is not
available.
At line:32 char:1
Invoke-ProcessASDatabase -databasename "MKTGCube" -server $ServerName ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Invoke-ProcessASDatabase], ArgumentException
FullyQualifiedErrorId : System.ArgumentException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ProcessASDatabase
I think the problem is linked to the credentials because we need to provide ones to access the source database but I have no ideas on how to do it through a PowerShell script. Any idea ?
Thanks a lot.
You need import module Azure.AnalysisServices and SqlServer to your automation account.
You could import these two module from this link and this link.
Note: There is a mistake in your script. You should use Login-AzureASAccount not Add-AzureRmAccount to login, you could use the following example:
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureAnalysisServicesAccount -RolloutEnvironment "southcentralus.asazure.windows.net" -ServicePrincipal -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint -TenantId $Conn.TenantID
Invoke-ProcessTable -Server "asazure://southcentralus.asazure.windows.net/myserver" -TableName "MyTable" -Database "MyDb" -RefreshType "Full"
More information about this please check this blog.
I need to create new mailboxes via Powershell in Office 365.
I am using this script:
$User = "administrator#blablabla.onmicrosoft.com"
$PWord = ConvertTo-SecureString -AsPlainText -Force -String "P#ssword1"
$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Import-CSV new.csv | foreach {
New-Mailbox -UserPrincipalName $_.UserPrincipalName -displayname $_.DisplayName -password (ConvertTo-SecureString $_.password -AsPlainText -Force) -usagelocation "us"
}
Get-PSSession | Remove-PSSession
Details of mailboxes are saved in new.csv file.
See the following example:
UserPrincipalName,DisplayName,password
clark.kent#blablabla.onmicrosoft.com,Clark Kent,P#ssword1
bruce.wayne#blablabla.onmicrosoft.com,Bruce Wayne,P#ssword1
peter.parker#blablabla.onmicrosoft.com,Peter Parker,P#ssword1
When I run this script, I return error:
A parameter cannot be found that matches parameter name 'UserPrincipalName'.
+ CategoryInfo : InvalidArgument: (:) [New-Mailbox],
ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,New-Mailbox
+ PSComputerName : outlook.office365.com
Please, what is wrong?
Can you help me?
The UPN Parameter is only available on the Exchange on-premises. Depending on on whether your AD is on-premises or in the cloud, I would suggest creating the AD account first with this parameter and then enable the mailbox.
Or just don't use this parameter.
Correct way to do this is by doing this via MicrosoftOnlineServicesID parameter which seems to replicate UPN.
New-Mailbox -Alias $mailbox.Alias -Name $mailbox.Name -DisplayName $mailbox.DisplayName -ResetPasswordOnNextLogon $true -Password $temporaryPassword -MicrosoftOnlineServicesID $upn -WhatIf
WARNING: After you create a new mailbox, you must go to the Office 365 Admin Center and assign the mailbox a license, or it will
be disabled after the grace period.
What if: Creating mailbox "Anna" with User Principal Name "anna#domain.org.pl" in organizational
unit "EURPR06A004.PROD.OUTLOOK.COM/Microsoft Exchange Hosted Organizations/domain.onmicrosoft.com".
Full story can be found at my blog but all you need is to change UPN to MicrosoftOnlineServicesID and it should work right away.
Try -identity instead of -userprincipalname
I'm attempting to use credential connection in a powershell script. I tried :
$credential = Get-Credential
and
$credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "Domain")
But when I execute a query, I get the following message :
Invoke-SqlQuery -Query $Query -Server $SERVER -database $DataBase -credential $credential
Exception calling "Open" with "0" argument(s): "Login failed for user '\sa'."
\Modules\InvokeSqlQuery\InvokeSqlQuery.psm1:155 char:14
+ $cnn.Open <<<< ();
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Even if I use the correct Credential, it fails. Has anyone had the same problem ?
Invoke-SqlQuery calls
$cnn = New-SqlConnection $Server $Database $Credential $ConnectionTimeout
which creates the error.
By the way, I would like to know how to load this assembly :
new-object [System.Management.Automation.PSCredential]
New-Object : Cannot find type [[System.Management.Automation.PSCredential]]: make sure the assembly containing this type is loaded.
Thanks.
To answer your second question :-), the assembly containing PSCredential is already loaded. It is an assembly required by PowerShell. Try this:
$username = ConvertTo-SecureString username -AsPlainText -Force
$password = ConvertTo-SecureString pass!word -AsPlainText -Force
$cred = new-object management.automation.pscredential $username,$password
try this (PsSnapin: SqlServerCmdletSnapin100):
$pwd = read-host -AsSecureString -Prompt "Password"
Invoke-Sqlcmd -Query $Query -Serverinstance $SERVER -database $DataBase –Username “MyLogin” –Password $pwd