Solved

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

Posted on 2015-02-05
2
255 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 79

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

803 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