Get-PnPProvisioningTemplate Execution error in Runbook - powershell

I ran the following Powershell in Runbook and got an error.
If there are any inappropriate parts in the logic, I would appreciate it if you could point them out.
Supplemental
 When I changed the name to Get-PnPFolderItem instead of Get-PnPProvisioningTemplate and executed it, it worked fine.
 Therefore, I think that Connect-PnPOnline is able to connect normally.
$TempSiteUrl = Get-AutomationVariable -Name 'SiteTemplateUrl'
$myCred = Get-AutomationPSCredential -Name SharePointConnect
Connect-PnPOnline -Url $TempSiteUrl -Credentials $myCred
Get-PnPProvisioningTemplate -Out template.xml
Error message
Get-PnPProvisioningTemplate : There is currently no connection yet. Use Connect-PnPOnline to connect.
At line:4 char:1
+ Get-PnPProvisioningTemplate -Out template.xml
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-PnPProvisioningTemplate], InvalidOperationException
+ FullyQualifiedErrorId :
System.InvalidOperationException,PnP.PowerShell.Commands.Provisioning.Site.GetProvisioningTemplate

Please try to use Get-PnPSiteTemplate instead: https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpsitetemplate?view=sharepoint-ps

Related

Running Windows updates from poweshell script on remote servers

I know this question has been asked a million times but I am really struggling with this and I would like to be able to add in a few extra features.
I have a list of servers which I would like to input into my script and then I would like the script to go off and check for updates on those servers. After which my aim is to get to it to ask if I want to continue giving the option to input Y/N.
Then I need it to go off and install updates without rebooting. It would be great then to tell me its finished and ask if I want to reboot.
Can anyone help? I am new to powershell and so far only got this which is erroring out.
$creds = Get-Credential
$serverlist = "C:\testlist.txt"
$session=New-PSSession -ComputerName $serverlist
Invoke-Command -Session $session -ScriptBlock {
[net.servicepointmanager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
Install-module pswindowsupdate -force -AllowClobber
Get-WUInstall -AcceptAll
}
Get-PSSession | Remove-PSSession`
This doesn't get me anywhere and gives this error.
New-PSSession : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.
At C:\Untitled5.ps1:16 char:10
+ $session=New-PSSession -ComputerName $serverlist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.String[]:String[]) [New-PSSession], ArgumentException
+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.NewPSSessionCommand
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the
command again.
At C:\Untitled5.ps1:17 char:25
+ Invoke-Command -Session $session -ScriptBlock {
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand`

Powershell Certificate Import From RootCA

With the GUI I just use MMC \ Certificates and do an import and point to my AD integrated certificate for ldaps, how can i accomplish this with powershell
I am trying this to no avail
Get-Certificate -Template ldaps -CertStoreLocation cert:\localmachine\MY -Credential $cred -Url http://rootca/certsrv
Error:
Get-Certificate : CX509EnrollmentPolicyWebService::Initialize: The parameter is incorrect. 0x80070057 (WIN32: 87 ERROR_INVALID_PARAMETER). This may be the result of user credentials being required on the remote machine. See Enable-WSManCredSSP Cmdlet help on
how to enable and use CredSSP for delegation with PowerShell remoting.
At line:1 char:1
+ Get-Certificate -Template Computerv2 -CertStoreLocation cert:\localma ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Certificate], Exception
+ FullyQualifiedErrorId : RemotingFailure,Microsoft.CertificateServices.Commands.GetCertificateCommand
certreq -enroll -machine -q "LDAPS_CERTIFICATE"

Remote server login error

Hi I want to login into my remote server using power shell . I wrote code for this but I am getting error .
CODE
$cred = get-credential - Prompts for username and password
Enter-PSSession -ComputerName servername -Credential $cred
ERROR
Get-Credential : A positional parameter cannot be found that accepts
argument 'Prompts'.At C:\documents\Untitled8.ps1:1 char:9
+ $cred = get-credential - Prompts for username and password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Credential], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetCredentialCommand
Enter-PSSession : Connecting to remote server XXXXX failed with the
following error message : Access is denied. For more information, see
the about_Remote_Troubleshooting Help topic.At
C:\documents\Untitled8.ps1:5 char:1
+ Enter-PSSession -ComputerName servername -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (servername:String) [Enter-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed
Any clue regarding this will help....
Get-Credential : A positional parameter cannot be found that accepts argument 'Prompts'
Anybody have any clue how to login into remote server in power shell using servername..any clue any link regarding this will be helpful
The problem is that there it can´t find a positional parameter, where "Prompts" is accepted. If you look at the help file for Get-credential you will see that the -Credential paramenter is positional, meaning you dont need to type it.
Try with this
$cred = get-credential -Message "Prompts for username and password"
Enter-PSSession -ComputerName servername -Credential $cred
Some reading about positional parameters
https://itknowledgeexchange.techtarget.com/powershell/positional-parameters/

"Access is denied" error during "New-Item" call in PS

I am trying to create a folder on a server using powershell. The script looks like this:
$pass = ConvertTo-SecureString "myPW" -AsPlainText -Force
$cred = new-object System.Management.Automation.PSCredential("myUser", $pass)
$session = New-PSSession -ComputerName "localhost" -Credential $cred
Invoke-Command -session $session -ScriptBlock {
New-Item -Path "\\myServer\myShare\" -Name "myFolder" -ItemType directory -Force
}
I'm getting the following error:
Access is denied
+ CategoryInfo : PermissionDenied: (\\myServer\myShare:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
+ PSComputerName : localhost
Access to the path 'myShare' is denied.
+ CategoryInfo : PermissionDenied: (\\myServer\myShare:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
+ PSComputerName : localhost
Access to the path 'myFolder' is denied.
+ CategoryInfo : PermissionDenied: (\\myServer\myShare\myFolder:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
+ PSComputerName : localhost
At first I thought the message is pretty clear an the user I used is missing rights. But here is the thing: If I go to the share in the Explorer and log in with the same user, I can create a folder without any problems. Besides that, the user is Domain Admin.
What am I doing wrong?
It's a double hop, you're connecting to a PSSession and then trying to access the folder.
As a security measure PowerShell does not allow you to remote to one computer, then remote to another computer from there (even if two of those are the same computer). The exact reasoning is complex and heavily involved in how credentials are used in sessions and I don't fully understand it myself so I won't try to explain but basically it's to prevent the credentials being stolen.
You can do some reading on it (this looks like a good resource) but it would be a LOT simpler to just try to work out another way of doing it. You're only remoting to the local computer so you could for example start another PS process.
If you are just using a PSSession for the alternative credentials to access the share, then using New-PSDrive might be an alternative way to do it:
$pass = ConvertTo-SecureString "myPW" -AsPlainText -Force
$cred = new-object System.Management.Automation.PSCredential("myUser", $pass)
New-PSDrive -Name myShare -PSProvider FileSystem -Root "\\myServer\myShare\" -Credential $cred
New-Item -Path "myShare:\" -Name "myFolder" -ItemType directory -Force

Azure AD - Powershell -Federation Metadata Url is missing for federated user

I am trying to Access my Azure AD using Powershell I am getting the below error
Powershell Error screenshot as below
Any one please help me to understand why i am getting this error.
Thanks in advance.
Error Message :
PS C:\Users\YYYYY> Connect-MsolService -Credential $password
Connect-MsolService : Authentication Error: Unable to complete
authentication request (potentially a proxy issue) At line:1 char:1
+ Connect-MsolService -Credential $password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Connect-MsolService], Exception
+ FullyQualifiedErrorId : System.Exception,Microsoft.Online.Administration.Automation.ConnectMsolService