Why does Enable-Mailbox show all mailbox properties when finished? - powershell

I have a Powershell script that creates AD Users from a CSV file and creates a mailbox in our on-premises Exchange server. The script works well, but every time it finishes the 'Enable-Mailbox' command, it outputs the mailbox properties as if 'Get-Mailbox | FL' is being called. The command is used as follows:
Enable-Mailbox -Identity <identity> -Alias <alias> -Database <database>
This link says that Enable-Mailbox has no output (https://learn.microsoft.com/en-us/exchange/client-developer/management/exchange-management-shell-cmdlet-input-and-output-types), and this command is at the end of the loop, so nothing else would be outputting anything before moving on to the next user.
Can anyone shed some light on what's going on or point me in the direction to figure out why this is happening?
EDIT: I'm piping to Out-Null to suppress the output for now

Related

Deleting an email using powershell

I want to delete an email from Exchange serveur and then from the mailbox received items,so I used this following powershell command but after a while , powershell_ise crashes and it closes
Search-Mailbox -Identity "Khalil Med" -SearchQuery 'Subject:"Phishing"' -DeleteContent
I live in the ISE daily and sometimes VSCode as needed and I do this sort of thing regularly in the ISE and VSCode, neither have crashed on me for this kind of use case. Yet, environments can differ. I find it hard to understand how running this simple request would crash the ISE, as it's not doing anything $psISE / ISE GUI specific.
Are you doing this on the Exchange server directly, or via Explicit or Implicit remote session from your admin workstation?
Also, make sure you have the RBAC role to perform this...
Get-ManagementRoleAssignment -Role 'Mailbox Import Export'
New-ManagementRoleAssignment -Role 'Mailbox Import Export' -User 'Administrator'
... and make sure you are getting something to act on, meaning actual emails using...
Search-Mailbox -Identity 'HostMaster TestUser' -SearchQuery "Subject:test" -EstimateResultOnly
If the response from the above is '0', then well, you now know why the -DeleteContent did not actually work.
As a sanity check. Rather than looking at one account. Look at all accounts to see if you are getting any hits on anyone..
Get-Mailbox -ResultSize Unlimited |
Search-Mailbox -SearchQuery 'Subject:test' -EstimateResultOnly -Force |
Where-Object -Property ResultItemsCount -gt 0

Check AD users' network share mapping with PowerShell

I tried using net use, net share, among others - none of which return the expected output. So instead, I'm modifying a script I found to see which network drives/shares are mapped to a user the script is pushed to. Then I go to my log file, look at the data, and determine if the account is set up properly. Here's the current script:
Get-WmiObject Win32_MappedLogicalDisk -computer $env:COMPUTERNAME | select name, providername
Out-File -filepath "\\*UNC filepath*\Mapped_Drives_$env:USERNAME$(get-date -Format _MM-dd-yy" # "HH.mm.ss" "tt).txt"
When I run it, the log file returns empty and I'm not sure why. I changed "Out-File -filepath" to "Start-Transcript" which isn't working the way I want it to either (with too much verbose output). It outputs fine in my PowerShell ISE with all the proper shares listed, but doesn't work when I navigate to the logged output. What am I missing?
You must pipe the output into the logfile
$logfile = "\\*UNC filepath*\Mapped_Drives_$env:USERNAME$(get-date -Format _MM-dd-yy" # "HH.mm.ss" "tt).txt"
Get-WmiObject Win32_MappedLogicalDisk | select name, providername | Out-File $logfile
On a more general note I'd use the commands to fix the mapped drives right there and then, instead of just writing them to a logfile for later inspection.

Office 365 Exchange Management Shell - Use data from Excel to run a loop with command

We are having round about 400 users that use Office 365 E-Mail Exchange which is managed on a local server via the Exchange Management Console. We activated E-Mail archives for the users that had PST-Files left on the server and migrated them by hand. To activate the mailbox archive we used the following command :
enable-remotemailbox <username> -archive
That worked fine and all. Now we want to double check and activate the archive for every user that has not been activated by hand. For this we have an excel sheet of all users with usernames which we want to run in a loop - so my question is :
Is it possible to tell the powershell to take information form an Excel/CSV file and loop through it with the above command (if an error occurs it needs to ignore and still run through). I imagined it somehow like
$users = Import-CSV C:\users.csv | foreach $user in $users
enable-remotemailbox $user -archive
Is it possible in this way or even an easier way?
You are almost there. Lets consider your CSV looks like this:
User
Alice
Bob
Then you can import the CSV and loop over each user using:
Import-Csv C:\users.csv | ForEach-Object {
enable-remotemailbox $_.User -archive
}

Using Read-Host to output information from a script

I am trying to run a script against exchange to bring back all of the mailboxes a certain user has access to. I want to be able to input the usersname using read-host. I currently have this:
$username = Read-Host("Please enter users username")
#Enable Exchange cmdlets
add-pssnapin *exchange* -erroraction SilentlyContinue
Get-MailBox | Get-MailboxPermission -User $username | FL > C:\MailboxPermissions.txt
However, when I run this via powershell, it asks for the username, looks like it is starting to run the script, then powershell just exits and there is not data outputted
Any help would be greatly appreciated
Thanks for all the help
I finally figured it out and there were a couple of issues. It was to do with the result size. I added -resultsize unlimited:
$username = Read-Host("Please enter users username")
add-pssnapin *exchange* -erroraction SilentlyContinue
>Get-MailBox -resultsize unlimited | Get-MailboxPermission -User $username | FL > C:\MailboxPermissions.txt
It would also not work by running the .ps1 file as this was not run by admin, and it needs admin permissions to output to the location I want. Once I created a shortcut for it to run via the powershell.exe with admin credentials it is now working as expected.
The problem is that you are only out putting to the screen.
This means that when you run your script it will carry out the required action, print to screen and close the window immidiatly. In turn, this means you can't see the output.
As #DarkLite1 mentioned, you could output to a file.
Or, you could simply allow the console to wait before closing. This is done like this at the end of your code:
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
You may also need a Write-Host on the last action in your code snippet, I'm not entirely sure as I am not familiar with how Get-Mailbox works, but try it without first.
To summarize, You must keep the window open or print the results to file to actually see anything. The code you have currently will complete so fast you will never see any output.

Powershell: Re-create RDS Remote Apps by Looping?

I'm stumped. I can usually take the output of one powershell command and use it as the input to another powershell command. For example:
Get-Mailbox | Set-Mailbox -MaxSendSize 40MB
This will loop through every mailbox and then set the maximum send size to 40 MB in Exchange 2007.
...but the same doesn't work with get-rdremoteapp and new-rdremoteapp.
Get-RDRemoteApp | new-rdremoteapp -collectionname APPSNEW -connectionbroker edge-1.mydom.local
The goal of this command is that we are preparing to Migrate from a Windows 2012 RDS environment on virtual servers to a Windows 2012 R2 environment on physical servers.
On the virtual 'edge' server, I should be able to get all the RD Remote Apps, loop through them, and then use the 'new-rdremoteapp' command to create them on the new 'edge-1' server.
What actually happens is the command runs and creates the 1st remote app, then exits without an error. It doesn't process the apps in the list.
I think I need to use foreach-object, but after reading the docs and playing around, I can't seem to get it to work.
I couldn't find an easy out. I had to specify a bunch of parameters like so:
Get-RDRemoteApp | foreach-object -process {new-rdremoteapp -collectionname APPSNEW -connectionbroker edge-1.mydom.local -displayname $_.displayname -filepath $_.filepath -alias $_.alias -commandlinesetting $_.commandlinesetting -usergroups $_.usergroups}
Time to find a job that has more bash scripting... ;)