Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-02-05
2
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

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

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

How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
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…
Suggested Courses

618 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