Currently I have this short piece of code. The URL https://dosomething.do generates a .csv file. I can run the powershell code and in the shell I can see the content of the file. However the file itself is not downloaded. I would like to specify where to save it after a download. What are my options here?
$securepassword = ConvertTo-SecureString "xxx" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("hxxx", $securepassword)
Invoke-WebRequest -Uri "https://dosomething.do" -Credential $credentials
Use the -OutFile parameter of Invoke-WebRequest:
Invoke-WebRequest -Uri "https://dosomething.do" -OutFile C:\some\path\to\yourfile.csv
Related
I'm trying to download config files from the bitbucket repository, using the RAW file link, but when the repository is set to private, it downloads file with HTML content like <!DOCTYPE html><html lang=" and not the actual file.
$userPassword = ConvertTo-SecureString -String $env:stashPassword -AsPlainText -Force
$credObject = New-Object System.Management.Automation.PSCredential ("testuser", $userPassword)
Invoke-RestMethod -Uri $urlAppsettings -OutFile c:\test\appSettings.config -Credential $credObject
The user 'testuser' has full permission on the bitbucket repository. Between, we are using a bitbucket server.
Any help here is really appreciable.
Thank you.
I got an old script that updates a pictures once per day on our SharePoint.
What the script did before the upgrade to SharePoint 365, is to make a photo with a webcam, saves the photo locally and then it uploads the photo to a SharePoint. It works fine.
But since we upgrade to SharePoint 365, the last step doesn't work anymore.
That is the old code for uploading it to SharePoint.
# Upload to Sharepoint
[System.Management.Automation.PSCredential] $SharepointCredentials = New-Object System.Management.Automation.PSCredential ($SharepointUser, ($SharepointPassword | ConvertTo-SecureString -AsPlainText -Force) )
Invoke-WebRequest -Uri $SharepointUrl -Method Put -InFile "$Path\webcam.jpg" -ContentType 'image/jpg' -Credential $SharepointCredentials -UseBasicParsing | Out-Null
I think that the problem is on Invoke-WebRequest and that Put doens't work anymore. Do you know if there is a new method to change or update the file?
This article explains how to upload files to Office 365 SharePoint library using CSOM via powershell. Hopefully that will get you on the right track.
https://blogs.technet.microsoft.com/fromthefield/2014/02/19/office-365-powershell-script-to-upload-files-to-a-document-library-using-csom/
Credentials parameter in Invoke-WebRequest cmdlet does not support SharePoint Online claims based authentication, it supports only authentication schemes such as basic, digest, NTLM, and Kerberos authentication.
To perform authenticated request in SharePoint Online using user credentials SharePointOnlineCredentials class could be utilized as demonstrated below:
function Connect-SPO ([string] $Username, [string]$Password, [string]$Url) {
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $(ConvertTo-SecureString -AsPlainText $Password -Force))
$Context.ExecuteQuery()
$AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($Url, $true)
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Credentials = $Context.Credentials
$WebSession.Cookies.SetCookies($Url, $AuthenticationCookie)
$FormsDigest = $Context.GetFormDigestDirect()
$WebSession.Headers['X-RequestDigest'] = $FormsDigest.DigestValue
$Context.Dispose()
return $WebSession
}
Then your script for uploading a file could be modified like this:
#Connect
$Session = Connect-SPO -Username $Username -Password $Password -Url $WebUrl
# Upload file
$targetFileUrl = "$WebUrl/documents/$($FileInfo.Name)" #set target file url
Invoke-WebRequest -Uri $targetFileUrl -Method Put -WebSession $Session -InFile $filePath
SPOUploadFile.ps1
While I use the method mentioned in this thread PowerShell's Invoke-RestMethod equivalent of curl -u (Basic Authentication) I could get connected to REST API without a 401 error.
However currently I am giving my password in Plain Text.
I want a way to use a Hash of my password and then use it in the script.
The script then should be able to decrypt it too. But I don't want others who have access to the script, be able to decrypt it.
So I don't want to expose the decryption algorithm as well to any.
Proposed method I am thinking of:
Combine existing HASH algorithms in a mixed random way (by feeding the HASH of one algorithm to another) which only I know and then have a custom Powershell function/cmdlet/whatever in the script which knows to decrypt.
Is there a simpler and better way?
Before I try the proposed method I would like to hear from others on any better ways.
Entire script is as below.
$User = "domain\userName"
$uri = "https://TeamProjectCollectionURI/TeamProjectName/_apis/build/builds"
$securePassword = ConvertTo-SecureString 'PasswordWhichContains$asWell' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($User, $securePassword)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$credential)))
$response = Invoke-RestMethod -Method Get -Uri $uri -Credential $credential -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType application/json
$response
You can pass a username and password (masked as a secret variable) through PowerShell into a PSCredential object and use the -Credential switch when invoking the REST method:
$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($User, $securePassword)
$releaseresponse = Invoke-RestMethod -Method Get -Credential $credential -ContentType application/json -Uri $Uri
More detail info please refer this blog: VSTS/TFS REST API: The basics and working with builds and releases
You can convert your password to encrypted string and use the encrypted string in your PowerShell script.
Convert to encrypted string:
$securePassword = ConvertTo-SecureString "Yourpassword" -AsPlainText -Force
$encryptedPwd = ConvertFrom-SecureString -SecureString $securePassword
Write-Host $encryptedPwd
Record the generated encrypted string and use it in your script.
$securePassword = ConvertTo-SecureString -String "Encrypted String"
I need to create a PowerShell script to upload a file to Archiva repository
I managed to do this by this sample:
$user = "user"
$pass = "password"
$secpass = ConvertTo-SecureString $pass -AsPlainText -Force
$uri = "http:*//X.X.X.X:8080/repository/win/test/test/1.6/test.exe"
$filePath = "x:\x\test.exe"
$cred = New-Object System.Management.Automation.PSCredential ($user, $secpass)
Invoke-RestMethod -Credential $cred -Uri $uri -Method Put -InFile $filePath -ContentType "multipart/form-data"
but it can't create or correct a maven-metadata.xml and a need that for version control. I want it to work like the upload trough Archiva UI.
So maybe somebody can help my with a script for upload using Archiva rest API by PowerShell.
I need to use this services from API
/archivaUiServices/fileUploadService
/fileUploadService/save/{repositoryId}/{groupId}/{artifactId}/{version}/{packaging}
, but don't know the correct way of using them in Powershell
(Managed to reproduce an upload by copping the request caught in fiddler)
Won't work as it.
You need to PUT (http method) your jar to the maven path.
http://yourinstantce/{repositoryId}/{groupId}/{artifactId}/{version}
I'm trying to write a PowerShell script to have a one click solution to uploading and building my mobile application. I have successfully done this using cURL but was trying to use native PowerShell commands instead. In cURL I can use the -F (--form) parameter and pass the zip file (e.g. -F file=#C:...\www.zip). I cannot figure out how to achieve this same thing using PowerShell. I am trying to use Invoke-RestMethod but not sure if this is correct. Here's a link to the PhoneGap API:
https://build.phonegap.com/docs/write_api
Any help would much appreciated!
Invoke-RestMethod -Uri https://build.phonegap.com/api/v1/apps/:id -Headers #{Authorization='Basic username-and-password-in-base64'} -Method Put -InFile "www.zip"
username-and-password-in-base64 is your Adobe/PhoneGap Build username and password combined into a string "username:password" and encoded using Base64 (http://www.base64decode.org/).
:id in the url is your app id in phonegap build.
Try something like this:
$pathToZip = "bin/phonegap.zip"
$user = "user"
$pass = "password"
## convert to secure password since we can't use get-credential in non interactive mode
$securepass = ConvertTo-SecureString $pass -AsPlainText -Force
## create a PSCredential object
$credential = New-Object System.Management.Automation.PSCredential($user, $securepass)
$appId = "12345"
$url = "https://build.phonegap.com/api/v1/apps/" + $appId;
Write-Host $url
Invoke-RestMethod -Uri $url -Credential $credential -Method Put -InFile $pathToZip