Little issue with New-ItemProperty in PowerShell - windows

I'm new to PowerShell and I can't seem to find how to fix this after countless Google searches. I know it's probably easy, but here's basically what I want to do and the error that shows:
PS C:\Windows\system32> $path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"
Get-Childitem $path -ErrorAction SilentlyContinue | Foreach {
$key = Get-Item $_.PSPath
if($key.Property -eq "VMnet") {
New-ItemProperty $key -name "*NdisDeviceType" -value "1"
}
}
New-ItemProperty : Cannot find path 'C:\Windows\system32\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0014' because it does not exist.
At line:7 char:25
+ New-ItemProperty <<<< $key -name "*NdisDeviceType" -value "1"
+ CategoryInfo : ObjectNotFound: (C:\Windows\syst...02BE10318}\0014:String) [New-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand
New-ItemProperty : Cannot find path 'C:\Windows\system32\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0015' because it does not exist.
At line:7 char:25
+ New-ItemProperty <<<< $key -name "*NdisDeviceType" -value "1"
+ CategoryInfo : ObjectNotFound: (C:\Windows\syst...02BE10318}\0015:String) [New-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewItemPropertyCommand
I clearly understand the error, it's obvious. But I don't know the proper way/command to fix it...

Try this:
$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\" +
"{4D36E972-E325-11CE-BFC1-08002BE10318}"
Get-Childitem $path -ErrorAction SilentlyContinue |
Where {(Get-ItemProperty $_.PSPath DriverDesc) -match 'VMnet' } |
Foreach {
New-ItemProperty $_.PSPath -name "*NdisDeviceType" -value "1"
}
}
BTW I don't see any regkeys for values named "Property" perhaps you could match on the DriverDesc reg value? Anyway, the reason you're getting the error is you have to specify the PSPath to New-ItemProperty ie in your script $key.PSPath.

Related

Disable Prelaunch of Microsoft Edge using PowerShell (Unexpected Token)

I should start off by stressing that I have very, very little experience in PowerShell, so apologies if this is user error.
After copying the commands over from https://learn.microsoft.com/en-us/troubleshoot/developer/browsers/administration/k-12-assessments-reports-apps-background I keep hitting Unexpected Token errors on step 4. This is the code, along with the errors:
$registryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\PreLaunch\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge" $Name = "Enabled" $value = "0" New-Item -Path $registryPath -Force | Out-Null New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD -Force | Out-Null
At line:1 char:138
... ch\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge" $Name = "Enab ...
~~~~~
Unexpected token '$Name' in expression or statement.
At line:1 char:156
... oftEdge_8wekyb3d8bbwe!MicrosoftEdge" $Name = "Enabled" $value = "0" N ...
~~~~~~
Unexpected token '$value' in expression or statement.
At line:1 char:169
... d8bbwe!MicrosoftEdge" $Name = "Enabled" $value = "0" New-Item -Path $ ...
~~~~~~~~
Unexpected token 'New-Item' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
I've tried messing around with a few things and have gotten rid of some, but not all of the errors. Any help would be greatly appreacited!
Sample formatting on that documentation article is broken, it's hiding a couple of linebreaks:
$registryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\PreLaunch\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"
$Name = "Enabled"
$value = "0"
New-Item -Path $registryPath -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD -Force | Out-Null

PowerShell Script For Creating Same Folders In Multiple Directories

Current folder structure:
\\Server\Source\A1\A1 101\
\\Server\Source\A1\A1 102\
\\Server\Source\A2\A2 101\
\\Server\Source\A2\A2 102\
\\Server\Source\A3\A3 101\
\\Server\Source\A3\A3 102\
I need to be able to create 3 folders, A, B, and C, inside each of the second level folders under source (A1 101, A2 101, etc.). FolderDir.txt has A, B, C on their own line. Is there a better way other than the following? There are a LOT more sub directories than listed above:
Foreach($folder in Get-Content "C:\temp\folderDir.txt")
{
New-Item "\\Server\Source\A1\A1 101\$folder" -ItemType directory
New-Item "\\Server\Source\A1\A1 102\$folder" -ItemType directory
New-Item "\\Server\Source\A2\A2 101\$folder" -ItemType directory
New-Item "\\Server\Source\A2\A2 102\$folder" -ItemType directory
New-Item "\\Server\Source\A3\A3 101\$folder" -ItemType directory
New-Item "\\Server\Source\A3\A3 102\$folder" -ItemType directory
}
Thanks! I'm still pretty new to PowerShell.
This is what I came up with:
Foreach($folder in Get-Content "C:\temp\folderDir.txt")
{
$lesson = Get-ChildItem -Path \\Server\Source\A*\*
New-Item $lesson\$folder -ItemType Directory
}
But I'm getting the error:
New-Item : Could not find a part of the path 'A'.
At line:4 char:1
+ New-Item $lesson\$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\\Server\Source...\A:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : Could not find a part of the path 'B'.
At line:4 char:1
+ New-Item $lesson\$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\\Server\Source...\B:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : Could not find a part of the path 'C'.
At line:4 char:1
+ New-Item $lesson\$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\\Server\Source...\C:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
However, if I put -WhatIf command on the end (after "directory") it appears to be able to do what I want...
Because the folderlist is iterated over several times it should be read in first
untested:
$folderlist = Get-Content "C:\temp\folderDir.txt"
Get-ChildItem -Path '\\Server\Source\*\*' -Directory | ForEach-Object {
ForEach ($folder in $folderlist){
New-Item (Join-Path $_.FullName $folder) -ItemType directory -Force -WhatIf
}
}
If the output look OK remove the -WhatIf

Remote Desktop Services: CAP or RAP through PowerShell

I am trying to configure a new RDS gateway server through Powershell (for automatic setup after EC2 creation). The issue I'm running into right now is setting up a default or otherwise CAP and RAP. Everything else seems to work just fine, and if I go through the server dialogs and point and click my way to doing the CAP/RAP wizard, it all works. Until I do so, those policies don't exist (not even a default).
The code I'm using, which I sourced from blog posts on technet about the subject, is this:
new-item -Force -Credential $AdminCredentials -path RDS:\GatewayServer\CAP -Name DomainAdmin-CAP -UserGroups “$AdminGroupName#$NetBiosDomainName" -AuthMethod 1
new-item -Force -Credential $AdminCredentials -path RDS:\GatewayServer\CAP -Name DomainUser-CAP -UserGroups “$UserGroupName#$NetBiosDomainName" -AuthMethod 1
new-item -Force -Credential $AdminCredentials -path RDS:\GatewayServer\RAP -Name DomainAdmin-RAP -UserGroups “$AdminGroupName#$NetBiosDomainName" -ComputerGroupType 2
new-item -Force -Credential $AdminCredentials -path RDS:\GatewayServer\RAP -Name DomainUser-RAP -UserGroups “$UserGroupName#$NetBiosdomainName" -ComputerGroupType 2
Again, everything else works and the system is 100% and useable once I RDP in to the server and set these up manually, so my only issue is this automation step. The error I'm getting when I run my script is this:
new-item : Access to the object at RDS:\GatewayServer\CAP\DomainAdmin-CAP is denied for the cmdlet
New-Item.The supplied value is not valid, or you do not have
sufficient permissions. At line:89 char:1
+ new-item -Force -Credential $AdminCredentials -path RDS:\GatewayServe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [New-Item], AccessViolationException
+ FullyQualifiedErrorId : PermissionDenied,Microsoft.PowerShell.Commands.NewItemCommand
Edit: Things I have tried based on suggestions and frustration:
I have made all " characters uniform after someone pointed out they were actually not the same thing on the front and rear of my -UserGroup variable strings - No error change.
I have tried $NetBiosDomainName as the simple one word NetBIOS version (DOMAIN) as well as the full domain (domain.company.com) - No error change.
I have tried changing the "$AdministratorsGroupName#$NetBiosDomainName" string out for $AdminGroup (= $AdministratorsGroupName + "#" + $NetBiosDomainName") to simplify the input to the -UserGroups parameter - No error change
I have run this script as both the domain admin and local administrator account - No error change
I was able to use the following script to configure a Remote Access Gateway on a non-domain Windows 2019/2016 server. This includes a CAP, a RAP, and a ManagedComputerGroup.
Install-WindowsFeature -Name "RDS-Gateway" -IncludeAllSubFeature -IncludeManagementTools > $null
Import-Module -Name RemoteDesktopServices
#=============User Modifiable=============#
$GroupName="SG_RemoteUsers#$env:COMPUTERNAME"
# No Restrictions
#$GroupName="Users#BUILTIN"
$ManagedComputers="RDG_RDCBComputers"
# Managed Computer Groups
$MCGComputers=#($env:COMPUTERNAME,"<target machines>")
$MCGs=#(#{Name="RDG_RDCBComputers";Desc="All RDCB Computers in the deployment";Computers=$MCGComputers})
# Connection Authorization Policies
$CAPs=#(#{Name="RDG_CAP_AllUsers";UserGroups=$GroupName;AuthMethod=1;Status=1})
# Resrouce Authorization Policies
$RAPs=#(#{Name="RDG_AllComputers";UserGroups=$GroupName;ComputerGroupType=2;ComputerGroup=$null})
# If you already have a certificate, skip this part.
<#region Certificate
$FilePath="C:\temp\export.cer"
$SelfSigned=New-SelfSignedCertificate -CertStoreLocation "Cert:\LocalMachine\My" -DnsName $env:COMPUTERNAME -FriendlyName $env:COMPUTERNAME -KeyAlgorithm "RSA" -HashAlgorithm "SHA256" -KeyDescription "RDG Key" -KeyLength 4096 -KeyUsage KeyEncipherment,DataEncipherment -KeyUsageProperty All
$CertPassword=ConvertTo-SecureString -String “password” -Force –AsPlainText
Export-PfxCertificate -Cert $SelfSigned.PSPath -FilePath $FilePath -Password $CertPassword > $null
Import-PfxCertificate -CertStoreLocation "Cert:\LocalMachine\Root" -FilePath $FilePath -Password $CertPassword > $null
Remove-Item -Path $FilePath
$Certificate=$SelfSigned
#endregion
#>
$Certificate=Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" } | Sort-Object -Property NotAfter -Descending | Select-Object -Last 1
#=============Do not modify=============#
$CAPPath="RDS:\GatewayServer\CAP"
$RAPPath="RDS:\GatewayServer\RAP"
$MCGPath="RDS:\GatewayServer\GatewayManagedComputerGroups"
#=============Script=============#
# Add the certificate to RDS
Set-Item -Path "RDS:\GatewayServer\SSLCertificate\Thumbprint" -Value $Certificate.Thumbprint
try { Get-LocalGroup -Name $GroupName -ErrorAction Stop > $null }
catch { New-LocalGroup -Name $GroupName > $null }
try { Get-LocalGroupMember -Group $GroupName -Member "<user that should have access>" -ErrorAction Stop > $null }
catch { Add-LocalGroupMember -Group $GroupName -Member "<user that should have access>" > $null }
# Remove existing items (must be done in the order of CAP and/or RAP first, then GatewayManagedComputerGroups
$CAPs | ForEach-Object { if (Test-Path -Path "$CAPPath\$($_.Name)") { Remove-Item -Path "$CAPPath\$($_.Name)" -Recurse } }
$RAPs | ForEach-Object { if (Test-Path -Path "$RAPPath\$($_.Name)") { Remove-Item -Path "$RAPPath\$($_.Name)" -Recurse } }
$MCGs | ForEach-Object { if (Test-Path -Path "$MCGPath\$($_.Name)") { Remove-Item -Path "$MCGPath\$($_.Name)" -Recurse } }
$MCGs | ForEach-Object { New-Item -Path $MCGPath -Name $_.Name -Description $_.Desc -Computers $_.Computers > $null }
$CAPs | ForEach-Object { New-Item -Path $CAPPath -Name $_.Name -UserGroups $_.UserGroups -AuthMethod $_.AuthMethod -Status $_.Status > $null }
$RAPs | ForEach-Object { New-Item -Path $RAPPath -Name $_.Name -UserGroups $_.UserGroups -ComputerGroupType $_.ComputerGroupType > $null }
# Stop redirection of Serial Ports
$CAPs | ForEach-Object { Set-Item -Path "$CAPPath\$($_.Name)\DeviceRedirection\SerialPorts" -Value 0 }
Restart-Service -Name "TSGateway"
This script, which shares a significant portion of the previous, is what I used to configure the same on a server that is a member of an AD domain. It's not the only way to do it, but hopefully it will give you a head start.
There is a portion of this script that creates the necessary AD group.
The only part that's missing is that the certificate must be placed in the Trusted Root store on the PC from which you want to connect.
Install-WindowsFeature -Name "RDS-Gateway" -IncludeAllSubFeature -IncludeManagementTools > $null
Import-Module -Name RemoteDesktopServices
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
#=============User Modifiable=============#
$GroupName="SG_RemoteUsers"
$ManagedComputers="RDG_RDCBComputers"
# Managed Computer Groups
$MCGComputers=#($env:COMPUTERNAME,"<target machines>")
$MCGs=#(#{Name=$ManagedComputers;Desc="All RDCB Computers in the deployment";Computers=$MCGComputers})
# Connection Authorization Policies
$CAPs=#(#{Name="RDG_CAP_AllUsers";UserGroups="$GroupName#$env:USERDOMAIN";AuthMethod=1;Status=1})
# Resrouce Authorization Policies
$RAPs=#(#{Name="RDG_AllDomainComputers";UserGroups="$GroupName#$env:USERDOMAIN";ComputerGroupType=1;ComputerGroup="Domain Computers#$env:USERDOMAIN"},
#{Name="RDG_RDConnectionBrokers";UserGroups="Domain Users#$env:USERDOMAIN";ComputerGroupType=0;ComputerGroup=$ManagedComputers})
# If you already have a certificate, skip this part.
<#region Certificate
$FilePath="C:\temp\export.cer"
$SelfSigned=New-SelfSignedCertificate -CertStoreLocation "Cert:\LocalMachine\My" -DnsName "$env:COMPUTERNAME.$env:USERDNSDOMAIN" -FriendlyName $env:COMPUTERNAME -KeyAlgorithm "RSA" -HashAlgorithm "SHA256" -KeyDescription "RDG Key" -KeyLength 4096 -KeyUsage KeyEncipherment,DataEncipherment -KeyUsageProperty All
$CertPassword=ConvertTo-SecureString -String “password” -Force –AsPlainText
Export-PfxCertificate -Cert $SelfSigned.PSPath -FilePath $FilePath -Password $CertPassword > $null
Import-PfxCertificate -CertStoreLocation "Cert:\LocalMachine\Root" -FilePath $FilePath -Password $CertPassword > $null
Remove-Item -Path $FilePath
$Certificate=$SelfSigned
#endregion
#>
$Certificate=Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" } | Sort-Object -Property NotAfter -Descending | Select-Object -Last 1
#=============Do not modify=============#
$CAPPath="RDS:\GatewayServer\CAP"
$RAPPath="RDS:\GatewayServer\RAP"
$MCGPath="RDS:\GatewayServer\GatewayManagedComputerGroups"
#=============Script=============#
# Add the certificate to RDS
Set-Item -Path "RDS:\GatewayServer\SSLCertificate\Thumbprint" -Value $Certificate.Thumbprint
$ADGroup=[System.DirectoryServices.DirectorySearcher]::new("(&(objectCategory=Group)(samAccountName=$GroupName))").FindOne()
if ($ADGroup -ne $null) {
$ADGroup=$ADGroup.GetDirectoryEntry()
$Parent=[System.DirectoryServices.DirectoryEntry]::new($ADGroup.Parent)
$Parent.Children.Remove($ADGroup)
$Parent.CommitChanges()
$Parent.Close()
}
$ADGroup=[System.DirectoryServices.AccountManagement.GroupPrincipal]::new([System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Domain),$GroupName)
$ADGroup.Description="Remote Gateway Users Group"
$ADGroup.GroupScope=[System.DirectoryServices.AccountManagement.GroupScope]::Global
$ADGroup.IsSecurityGroup=$true
$Member=[System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity([System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Domain),"Administrator")
$ADGroup.Members.Add($Member)
$Member=[System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity([System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Domain),"admin_jeberhardt")
$ADGroup.Members.Add($Member)
$ADGroup.DisplayName=$ADGroup.SamAccountName
$ADGroup.Save()
$OU=[System.DirectoryServices.DirectoryEntry]::new("LDAP://$($ADGroup.DistinguishedName)")
$NewOU=[System.DirectoryServices.DirectoryEntry]::new("LDAP://OU=<target OU>,DC=<domain name>,DC=com")
$OU.MoveTo($NewOU)
$OU.CommitChanges()
$OU.Close()
$NewOU.Close()
# Remove existing items (must be done in the order of CAP and/or RAP first, then GatewayManagedComputerGroups
$CAPs | ForEach-Object { if (Test-Path -Path "$CAPPath\$($_.Name)") { Write-Verbose "Removing $($_.Name)"; Remove-Item -Path "$CAPPath\$($_.Name)" -Recurse } }
$RAPs | ForEach-Object { if (Test-Path -Path "$RAPPath\$($_.Name)") { Write-Verbose "Removing $($_.Name)"; Remove-Item -Path "$RAPPath\$($_.Name)" -Recurse } }
$MCGs | ForEach-Object { if (Test-Path -Path "$MCGPath\$($_.Name)") { Write-Verbose "Removing $($_.Name)"; Remove-Item -Path "$MCGPath\$($_.Name)" -Recurse } }
$MCGs | ForEach-Object { Write-Verbose "Creating $($_.Name)"; New-Item -Path $MCGPath -Name $_.Name -Description $_.Desc -Computers $_.Computers > $null }
$CAPs | ForEach-Object { Write-Verbose "Creating $($_.Name)"; New-Item -Path $CAPPath -Name $_.Name -UserGroups $_.UserGroups -AuthMethod $_.AuthMethod -Status $_.Status > $null }
$RAPs | ForEach-Object { Write-Verbose "Creating $($_.Name)"; New-Item -Path $RAPPath -Name $_.Name -UserGroups $_.UserGroups -ComputerGroup $_.ComputerGroup -ComputerGroupType $_.ComputerGroupType > $null }
# Stop redirection of Serial Ports
$CAPs | ForEach-Object { Set-Item -Path "$CAPPath\$($_.Name)\DeviceRedirection\SerialPorts" -Value 0 }
Restart-Service -Name "TSGateway"

Powershell script to change the Recycling Time on Application Pool

I found the code that I think that i need to use, but the thing is that it is not working.
Import-Module WebAdministration
$appPools = Get-childItem 'IIS:\AppPools\App Pool'
Set-ItemProperty -Path $appPools -Name recycling.periodicRestart.time -Value 1.00:00:00
But I am getting this error
Set-ItemProperty : Cannot find path 'C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\WebAdministration\Microsoft.IIs.PowerShell.Framework.NodeCollection' because it does not exist.
At line:3 char:1
+ Set-ItemProperty -Path $appPools -Name recycling.periodicRestart.time -Value 1.0 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Windows\SysW....NodeCollection:String) [Set-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand
I know it isn't a path issue. This does work.
set-itemproperty -path 'D:\test\TestPS\New Text.txt' -name IsReadOnly -value $true
Any help would be great...
It is a path issue.
The object returned from Get-ChildItem 'IIS:\AppPools\App Pool' is a NodeCollection object, and when you run Set-ItemProperty -Path $appPools, $appPools is expanded to "Microsoft.IIs.PowerShell.Framework.NodeCollection" (which is not a valid path)
To change the properties of the app pool:
Set-ItemProperty -Path 'IIS:\AppPools\App Pool' -Name recycling.periodicRestart.time -Value 1.00:00:00

Trying to open Outlook DefaultProfileName only

Could someone please suggest how I can $Outlook.Application.DefaultProfileName without breaking the variables.
Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\FindOutlookPSTsMike.ps1:14 char:21
+ $Object | Add-Member <<<< -MemberType NoteProperty -Name 'ComputerName' -Value $ComputerName
+ CategoryInfo : InvalidData: (:) [Add-Member], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCo
mmand
Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\FindOutlookPSTsMike.ps1:15 char:21
+ $Object | Add-Member <<<< -MemberType NoteProperty -Name 'UserName' -Value $UserName
+ CategoryInfo : InvalidData: (:) [Add-Member], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCo
mmand
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\FindOutlookPSTsMike.ps1:16 char:21
+ $Object | Export-Csv <<<< -NoTypeInformation \UK-PR-EXMC02\PSTLOG\$UserName-$ComputerName-OpenPSTs-$Date.csv
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
mmand
$Date = Get-Date -format d-M-yyyy
$UserName = $env:USERNAME
$ComputerName = $env:COMPUTERNAME
$Outlook = New-Object -comObject Outlook.Application
$Profile = $Outlook.Application.DefaultProfileName
$Object = $Profile.Session.Stores | Where {$_.FilePath -like "*.PST"} | Select `
#{Expression={$_.DisplayName}; Label="PST Name in Outlook"},`
#{Expression={$_.FilePath}; Label="PST Location/FileName"},`
#{Expression={$_.IsOpen}; Label="PST Open in Outlook"},`
#{Expression={(Get-Item $_.FilePath).Length / 1KB}; Label="PST File Size (KB)"}
$Object | Add-Member -MemberType NoteProperty -Name 'ComputerName' -Value $ComputerName
$Object | Add-Member -MemberType NoteProperty -Name 'UserName' -Value $UserName
$Object | Export-Csv -NoTypeInformation \\<NetworkShareName>\$UserName-$ComputerName-OpenPSTs-$Date.csv
Start-Sleep 5
Get-Process | Where {$_.Name -like "Outlook*"} | Stop-Process
Application.DefaultProfileName returns a string with the name of the profile, not an object. Are you trying to enumerate PST files in a particular profile?

Resources