Solved

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

Posted on 2015-02-05
2
269 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 80

Accepted Solution

by:
David Johnson, CD, MVP earned 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

808 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