?
Solved

Problem with exporting credentials in Powershell to a .xml file.

Posted on 2015-02-05
2
Medium Priority
?
345 Views
Last Modified: 2015-02-16
First question:
I am having problems with getting powershell to export my credentials to a file. I am running this:
Get-Credential "admin@ScriptingGuy.OnMicrosoft.Com" |
Export-Clixml c:\fso\ScriptingGuyCredential.xml
I have used Azure active directory module for windows powershell and Azure powershell and both wont export my creds to a file to refer to.

Second question.
I am trying to write a ps1 file to run from powershell to make things easier. When we add a new user to active directory it syncs to 365. Nobody can see the busy/free details of this user even though every one is set to Reviewer. The only way I have been able to show the details is by running a ps1 file after logging in with creds every time a new user comes on board. This is what I run.

$Creds = Get-Credential
(user@domain.onmicrosoft.com
Password)

get-executionpolicy

set-executionpolicy unrestricted      (answer yes)

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic –AllowRedirection

Import-PSSession $Session
      
Import-Module MSOnline

Connect-MsolService –Credential $Creds
      
$onPrem = get-credential
(Differentdomain\differentuser
Password)

Then I run this PS1 file called outlookfix.ps1 This is the file.

foreach($mbx in Get-Mailbox -ResultSize Unlimited | where-object {$_.displayname -notmatch "discovery"}) {
    $mbox = $mbx.alias+":\calendar"
    $test = Get-MailboxFolderPermission -Identity $mbox -erroraction silentlycontinue
    if($test -ne $null){
        Set-MailboxFolderPermission -Identity $mbox -User Default -AccessRights Reviewer | out-null
        [string]$user=$mbx.name
        [string]$folder="Calendar"
        [string]$AccessRight="Applied"
        $obj = New-Object PSObject
        $obj | Add-Member NoteProperty User ($user)
        $obj | Add-Member NoteProperty Folder ($folder)
        $obj | Add-Member NoteProperty AccessRight ($AccessRight)
        Write-Output $obj

I want to combine the stuff into one ps1 file that I can call upon in powershell to automate the process.
Is it possible to get the first creds and the second creds put into 2 diff files so that I can minimize the amount of scripts I have to put in.

Thanks in advance.
0
Comment
Question by:bobgraf
2 Comments
 
LVL 84

Accepted Solution

by:
David Johnson, CD, MVP earned 1500 total points
ID: 40592406
it will never be an .xml file
if(!(test-path "c:\test\username.txt"))
    {
    $username = read-host "Username"
    $username | out-file -FilePath c:\test\username.txt
    }
if(!(test-path "c:\test\cred.txt"))
    {
    read-host "Password" -assecurestring | convertfrom-securestring | out-file C:\test\cred.txt
    }
$password = get-content c:\test\cred.txt | convertto-securestring
$username = get-content -Path C:\test\username.txt
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Authentication Basic -AllowRedirection -Credential $credentials
#Import-PSSession $O365Session
Connect-MsolService -Credential $credentials

foreach($mbx in Get-Mailbox -ResultSize Unlimited | where-object {$_.displayname -notmatch "discovery"}) {
    $mbox = $mbx.alias+":\calendar"
    $test = Get-MailboxFolderPermission -Identity $mbox -erroraction silentlycontinue
    if($test -ne $null){
        Set-MailboxFolderPermission -Identity $mbox -User Default -AccessRights Reviewer | out-null
        [string]$user=$mbx.name
        [string]$folder="Calendar"
        [string]$AccessRight="Applied"
        $obj = New-Object PSObject
        $obj | Add-Member NoteProperty User ($user)
        $obj | Add-Member NoteProperty Folder ($folder)
        $obj | Add-Member NoteProperty AccessRight ($AccessRight)
        Write-Output $obj
        }
}

Open in new window

0
 

Author Comment

by:bobgraf
ID: 40612819
Thanks for the assistance. The import creds didn't work and its possible that I might not be doing it right. I was able to write a script regardless and it's working. Thanks for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Steps to fix error: “Couldn’t mount the database that you specified. Specified database: HU-DB; Error code: An Active Manager operation fail”
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video discusses moving either the default database or any database to a new volume.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question