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?
Related
I have app pools on the IIS web server with settings that I have been able to get out in a CSV file but I have a need to change some settings based on that CSV file extracted previously.
How do I configure the settings with powershell scripts without going from server to server? Here is a sneak peak of the script I have written:
$IISInstalled = (Get-WindowsFeature -Name "Web-Server").installed
If ($IISInstalled -ieq $false){
Write-Output "IIS not installed"
exit
}
Import-Module webadministration
$hostname = hostname
$allapppools = (Set-Item IIS:\AppPools\).name
$path = "c:\appsettings\"
if(!(test-path $path))
{
New-Item -ItemType Directory -Force -path $path
}
$allapppools | ForEach-Object($_) {
$apppool=Set-ItemProperty IIS:\AppPools\$_
$obj = New-Object PSObject
$obj | Add-Member Hostname $hostname
$obj | Add-Member AppPoolName $apppool.Name
$obj | Add-Member .NETCLRVer $apppool.ManagedRuntimeVersion
$obj | Add-Member 32-bit $apppool.Enable32BitAppOnWin64
$obj | Add-Member ManagedPipeline $apppool.ManagedPipelineMode
$obj | Add-Member QueueLength $apppool.QueueLength
$obj | Add-Member StartupMode $apppool.StartMode
$obj | Add-Member recycling-periodicRestarttime $apppool -Name recycling.periodicRestart.time -Value '0.23:00:00'
$obj | Add-Member recycling-periodicRestartHours $apppool -Name recycling.periodicRestart.time.Hours -Value '23'
$obj | Add-Member recycling-periodicRestartDays $apppool -Name recycling.periodicRestart.time.Days -Value '0'
$obj | Add-Member recycling-periodicRestartMinutes $apppool -Name recycling.periodicRestart.time.Minutes -Value '0'
$obj | Add-Member recycling-periodicRestartTotalHours $apppool -Name recycling.periodicRestart.time.TotalHours -Value '23'
$obj | Add-Member recycling-periodicRestartTotalMinutes $apppool -Name recycling.periodicRestart.time.TotalMinutes -Value '1380'
$obj | Add-Member recycling-periodicRestartTotalDays $apppool -Name recycling.periodicRestart.time.TotalDays -Value '0.958333'
$obj | Add-Member ItemXPath $apppool.ItemXPath
$obj | Add-Member PSPath $apppool.PSPath
$obj | Add-Member PSParentPath $apppool.PSParentPath
$obj | Add-Member PSChildName $apppool.PSChildName
$obj | Add-Member PSDrive-Root $apppool.PSDrive.Root
$obj | export-csv \\w2-cmn-util02\Reports\app-pool-azure-20210729.csv -NoTypeInformation -append
}
I understand I might have to change the scripts to effect changes on the server but I do not know how this should be done, please I'd need some help. Could someone advice on the working way to make changes on the some pools and not all.
Thanks
I am very new to using powershell and trying to execute a script that scans the entire network for all .mdb and .accdb files, for example, and generates a spreadsheet containing the data on them that I process elsewhere.
I put the sensistive data that I didnt want to provide in ()s
Here is my code:
#single threaded
import-module activedirectory
$arr = #()
$computers = Get-ADComputer -filter 'name -like "(employee computers)*"' | Select -Exp Name
foreach ($computer in $computers) {
Write-Host "Scanning" $computer "..."
gci \\$computer\c$\* -Include *.mdb, *.accdb -Recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Size $_.Length
$obj | Add-Member NoteProperty CreationTime $_.CreationTime
$obj | Add-Member NoteProperty LastWriteTime $_.LastWriteTime
$arr += $obj
Write-Host "Scanning..."
}}
$arr | Export-CSV -notypeinformation '(path)\EmployeeDBs.csv'
This has been working pretty well so far, but for certain machines and/or directories on some machines I am receiving the following error messages:
Get-ChildItem : The specified network name is no longer available
[Get-ChildItem], IOException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand
and
Get-ChildItem : An object at the specified path \\(employee computer)\c$ does not exist.
[Get-ChildItem], IOException
+ FullyQualifiedErrorId : ItemDoesNotExist,Microsoft.PowerShell.Commands.GetChildItemCommand
I have been googling around but havent had much luck in understanding these error messages. Would somebody be able to explain what the issues are?
I am thinking (hoping) that they are permissions problems because I am testing the scripts on my personal machine before I run them from the admin machine
Any insight is greatly appreciated!
EDIT: below is my edited code for asynchronous execution:
import-module activedirectory
$computers = Get-ADComputer -filter 'name -like "wa-150*"' | Select -Exp Name
Get-job | Remove-Job -Force
Remove-Item -path (path)\EmployeeDBs.txt
foreach ($computer in $computers) {
$scriptBlock = {gci \\$($args[0])\c$\Users\z*\Desktop\* -Include *.mdb, *.accdb -Recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size $_.Length
$obj | Add-Member NoteProperty CreationTime $_.CreationTime
$obj | Add-Member NoteProperty LastWriteTime $_.LastWriteTime
Write-Output -InputObject $obj
}
}
while ((Get-Job -State Running).Count -ge 20) {
Write-Host "Full - Waiting ... "
Start-Sleep -Seconds 5;
}
Start-Job -name $computer -ScriptBlock $scriptBlock -ArgumentList $computer
#Invoke-Command -ScriptBlock $scriptBlock -ArgumentList $computer
}
Get-Job | Wait-Job | Receive-Job | Out-File -Append -FilePath '(path)\EmployeeDBs.txt'
Write-Host "Done"
when calling the function below, it doesn't display output on "Windows PowerShell console" but on "Windows Powershell ISE" can.
Function DNSSearchOrders{
$Computer = $env:computername
$Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer
$DNSServers = $Networks.DNSServerSearchOrder
$NetworkName = $Networks.Description
$PrimaryDNSServer = $DNSServers[0]
$SecondaryDNSServer = $DNSServers[1]
$TertiaryDNSServer = $DNSServers[2]
$OutputObj = #()
$NewObj = New-Object -Type PSObject
$NewObj | Add-Member -MemberType NoteProperty -Name Domain -Value $Computer.ToUpper()
$NewObj | Add-Member -MemberType NoteProperty -Name PrimaryDNSServers -Value $PrimaryDNSServer
$NewObj | Add-Member -MemberType NoteProperty -Name SecondaryDNSServers -Value $SecondaryDNSServer
$NewObj | Add-Member -MemberType NoteProperty -Name TertiaryDNSServers -Value $TertiaryDNSServer
$OutputObj += $NewObj
$NewObj = New-Object -Type PSObject
$NewObj | Add-Member -MemberType NoteProperty -Name Domain -Value contoso.com
$NewObj | Add-Member -MemberType NoteProperty -Name PrimaryDNSServers -Value 111.111.88.88
$NewObj | Add-Member -MemberType NoteProperty -Name SecondaryDNSServers -Value 222.333.444.88
$NewObj| Add-Member -MemberType NoteProperty -Name TertiaryDNSServers -Value "DC IP Address"
$OutputObj += $NewObj
$OutputObj
}
DNSSearchOrders
I would like to know how to disable Automatic Updates with PowerShell on a windows machine.
Thanks in advance!
Here's a couple functions to set and get Windows Update configurations
$SCRIPT:AutoUpdateNotificationLevels= #{
0="Not configured";
1="Disabled";
2="Notify before download";
3="Notify before installation";
4="Scheduled installation"
}
$SCRIPT:AutoUpdateDays=#{
0="Every Day";
1="Every Sunday";
2="Every Monday";
3="Every Tuesday";
4="Every Wednesday";
5="Every Thursday";
6="Every Friday";
7="Every Saturday"
}
Function Get-WindowsUpdateConfig
{
$AUSettings = (New-Object -com "Microsoft.Update.AutoUpdate").Settings
$AUObj = New-Object -TypeName System.Object
Add-Member -inputObject $AuObj -MemberType NoteProperty -Name "NotificationLevel" `
-Value $AutoUpdateNotificationLevels[$AUSettings.NotificationLevel]
Add-Member -inputObject $AuObj -MemberType NoteProperty -Name "UpdateDays" `
-Value $AutoUpdateDays[$AUSettings.ScheduledInstallationDay]
Add-Member -inputObject $AuObj -MemberType NoteProperty -Name "UpdateHour" `
-Value $AUSettings.ScheduledInstallationTime
Add-Member -inputObject $AuObj -MemberType NoteProperty -Name "Recommended updates" `
-Value $(IF ($AUSettings.IncludeRecommendedUpdates) {"Included"} else {"Excluded"})
$AuObj
}
Function Set-WindowsUpdateConfig
{
Param (
[Parameter()]
[ValidateRange(0,4)]
[int]
$NotificationLevel ,
[Parameter()]
[ValidateRange(0,7)]
[int]
$Day,
[Parameter()]
[ValidateRange(0,24)]
[int]
$hour,
[Parameter()]
[bool]
$IncludeRecommended
)
$AUSettings = (New-Object -com "Microsoft.Update.AutoUpdate").Settings
if ($NotificationLevel) {$AUSettings.NotificationLevel =$NotificationLevel}
if ($Day) {$AUSettings.ScheduledInstallationDay =$Day}
if ($hour) {$AUSettings.ScheduledInstallationTime=$hour}
if ($IncludeRecommended) {$AUSettings.IncludeRecommendedUpdates=$IncludeRecommended}
$AUSettings.Save()
}
Here is a link and how to set the registry settings
# http://support.microsoft.com/kb/328010
New-Item HKLM:\SOFTWARE\Policies\Microsoft\Windows -Name WindowsUpdate
New-Item HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate -Name AU
New-ItemProperty HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name NoAutoUpdate -Value 1
you can use CMD command:
sc stop wuauserv
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.