Check every computer in network if specific service runs - c#

For a software update, I need to know if a specific service runs on all computers. And if not, I need to start this service on the missing devices.
Is there a possibility to realize this in C# or PowerShell?

In PowerShell you'd use the *-Service cmdlets. Get-Service can query services on remote hosts via its -ComputerName parameter. The returned services can then be filtered and piped into Start-Service:
$servers = 'FOO', 'BAR', 'BAZ', ...
Get-Service -ComputerName $servers -Name 'svcname' |
? { $_.Status -eq 'Stopped' } |
Start-Service

How to find computers running a particular service has already been answered here:
how to get a pc list that have a service running on each pc?
you should then be able to run Start-Service -Name <Service Name>

I ended up with a mix of all suggestions here and wrote a powershell script.
Any improvements are much appreciated.
$strFilter = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.SearchScope = "Subtree"
$objSearcher.PageSize = 1000
$objSearcher.Filter = "(objectCategory=$strFilter)"
$colResults = $objSearcher.FindAll()
$counter = 0
foreach ($i in $colResults)
{
$objComputer = $i.GetDirectoryEntry()
if (Test-Connection -Computername $objComputer.Name -BufferSize 16 -Count 1 -Quiet) {
Get-Service -ComputerName $objComputer.Name -Name 'svcname' | ? { $_.Status -eq 'Stopped' } | Start-Service
}
$counter++
Write-Host "finished " + $counter
}

Related

Deleting file from FTP server using PowerShell

I wrote a PowerShell script to download files using FTPto my local machine.
After the file is downloaded, I want to delete it from the FTP server. I wrote this code too. But unfortunately it's not working.
Can anyone help me to point out what is wrong with my code? Any clues will be helpful ...
Here is my code
function Delete-File($Source,$Target,$UserName,$Password)
{
$ftprequest = [System.Net.FtpWebRequest]::create($Source)
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($UserName,$Password)
if(Test-Path $Source)
{
"ABCDEF File exists on ftp server."
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftprequest.GetResponse()
"ABCDEF File deleted."
}
}
function Get-FTPFile ($Source,$Target,$UserName,$Password)
{
# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($Source)
# set the request's network credentials for an authenticated connection
$ftprequest.Credentials =
New-Object System.Net.NetworkCredential($username,$password)
if(Test-Path $targetpath)
{
"ABCDEF File exists"
}
else
{
"ABCDEF File downloaded"
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false
Delete-File $sourceuri $targetpath $user $pass
}
# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()
# get a download stream from the server response
$responsestream = $ftpresponse.GetResponseStream()
# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024
# loop through the download stream and send the data to the target
file
do{
$readlength = $responsestream.Read($readbuffer,0,1024)
$targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)
$targetfile.close()
}
$sourceuri = "ftp://ftpxyz.com/vit/ABCDEF.XML"
$targetpath = "C:\Temp\M\NEWFOLDER\ABCDEF.XML"
$user = "*******"
$pass = "*******"
Get-FTPFile $sourceuri $targetpath $user $pass
Delete-File $sourceuri $targetpath $user $pass
Every time I execute this script, the only statement I get
ABCDEF file downloaded
or
ABCDEF file exists
I guess Delete-File is not executing at all... any type of clue will be helpful.
You cannot use Test-Path with an FTP URL. So your code for deleting the file will never execute.
Just remove the Test-Path condition and try to delete the file unconditionally. Then check for error and treat "file not exist" error as you like.
$ftprequest = [System.Net.FtpWebRequest]::create($Source)
$ftprequest.Credentials =
New-Object System.Net.NetworkCredential($UserName, $Password)
try
{
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftprequest.GetResponse() | Out-Null
Write-Host ("File {0} deleted." -f $Source)
}
catch
{
if ($_.Exception.InnerException.Response.StatusCode -eq 550)
{
Write-Host ("File {0} does not exist." -f $Source)
}
else
{
Write-Host $_.Exception.Message
}
}
Though as you try to delete the file only after you successfully download it, it's actually unlikely that the file won't exist.
So you may consider to give up on any specific error handling.
I ran your script locally to try it out and found a few issues. I refactored also a few things just to make it a bit more readable (at least in my opinion :) ).
Issues
Line 13. $Source parameter there is a ftp://... path. Test-Path will always return $false here and the delete request will never be executed.
In Get-FTPFile you were not referencing the input parameter of the function, instead the variables defined outside of it. I don't know if this was just a copy & paste bug or on purpose. In my opinion you should use the parameters you sent to the function. Lines 38, 39 and 50 at least in my code below.
Code
function Delete-File
{
param(
[string]$Source,
[string]$Target,
[string]$UserName,
[string]$Password
)
$ftprequest = [System.Net.FtpWebRequest]::create($Source)
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($UserName,$Password)
if(Test-Path $Source)
{
"ABCDEF File exists on ftp server."
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftprequest.GetResponse()
"ABCDEF File deleted."
}
}
function Get-FTPFile
{
param(
[string]$Source,
[string]$Target,
[string]$UserName,
[string]$Password
)
# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($Source)
# set the request's network credentials for an authenticated connection
$ftprequest.Credentials =
New-Object System.Net.NetworkCredential($UserName,$Password)
if(Test-Path $Target)
{
"ABCDEF File exists"
}
else
{
"ABCDEF File downloaded"
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false
Delete-File $Source $Target $UserName $Password
}
# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()
# get a download stream from the server response
$responsestream = $ftpresponse.GetResponseStream()
# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024
# loop through the download stream and send the data to the target
file
do{
$readlength = $responsestream.Read($readbuffer,0,1024)
$targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)
$targetfile.close()
}
$sourceuri = "ftp://ftpxyz.com/vit/ABCDEF.XML"
$targetpath = "C:\Temp\M\NEWFOLDER\ABCDEF.XML"
$user = "*******"
$pass = "*******"
Get-FTPFile $sourceuri $targetpath $user $pass
#Delete-File $sourceuri $targetpath $user $pass
There are also ready made PowerShell cmdlets for talking to FTP/SFTP, no need to create everything from scratch, unless you are required to.
Anyway, for reference, check out e.g.
https://www.powershellgallery.com/packages/PSFTP
https://www.powershellgallery.com/packages/WinSCP

outlook powershell replied email property

Hi I am creating a powershell script to read e-mail from outlook on which i have replied. can someone help me to find out the property in the variable.
all emails are in $monitor variable.
Add-type -assembly “Microsoft.Office.Interop.Outlook” | out-null
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$Monitor = $folder.Folders.Item("Test")
From https://stackoverflow.com/a/15323686/478656 and comments at https://www.slipstick.com/developer/code-samples/forward-messages-not-replied/ it looks like you want
$Email.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003")
Which is the property for PR_LAST_VERB_EXECUTED, and the output is either 0 (not replied), 102 ('Reply') or 103 ('Reply All').
So maybe
$LastVerb = "http://schemas.microsoft.com/mapi/proptag/0x10810003"
$Monitor.Items | Where-Object { $_.PropertyAccessor.GetProperty($LastVerb) -gt 0 }

Get if a current script Powershell is executed as Administrator (Run as Administrator)

I have Windows Service C# with credentials for user UserInstallerMoss.
Windows Service execute an EXE Console Aplicaction C# with credentials UserInstallerMoss.
EXE Console Aplicaction executes powershell.exe with credentials UserInstallerMoss.
Server is Windows Server 2012 Enterprise. UAC is disabled.
UserinstallerMOss is local administrator
Powershell functions returns $true value:
$ok = IsCurrentUserAdmin
$ok = IsCurrentUserAdmin2
but the script fails about "access denied"
nativehr: 0x80070005 OWSSVR.DLL - Access denied
How can I get if a current script Powershell is executed as Run as Administrator?
How can I get if a current user in script Powershell is Administrator?
My functions returns true, but maybe it was wrong?
Powershell functions:
Function IsCurrentUserAdmin
{
$ident = [Security.Principal.WindowsIdentity]::GetCurrent()
foreach ( $groupIdent in $ident.Groups )
{
if ( $groupIdent.IsValidTargetType([Security.Principal.SecurityIdentifier]) )
{
$groupSid = $groupIdent.Translate([Security.Principal.SecurityIdentifier])
if ( $groupSid.IsWellKnown("AccountAdministratorSid") -or $groupSid.IsWellKnown("BuiltinAdministratorsSid"))
{
return $TRUE
}
}
}
return $FALSE
}
Function IsCurrentUserAdmin2
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
If you want to test if script is running elevated:
function Test-RunningElevated{
# returns True if running elevated, otherwise returns False
$windowsIdentity=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$windowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($windowsIdentity)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
Write-Output ($windowsPrincipal.IsInRole($adm))
}
IF you want to test if user running script is member of local administrators group:
function Test-LocalAdministrator{
$currentUser = $env:USERNAME
$currentComputer = $env:COMPUTERNAME
$adminGroup = [ADSI]"WinNT://$currentComputer/Administrators"
$adminGroup.Members() | ForEach-Object{
$member = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
if($member -eq $currentUser){
Write-Output $true
break
}
}
Write-Output $false
}
Use them like this:
if(Test-RunningElevated){
# code to run goes here
}
else{
Write-Warning 'This script needs to be run elevated!'
}
Note that the Test-LocalAdministrator only checks direct memberships.

Call WinRT Async method from Powershell to set account picture in win8

I'm trying to put something together that will use the AD Thumbnail photo to set a user's account picture on Windows 8. It seems like I should be able to use the API from WinRT to do this. I've pieced something together from various sources about calling the API from powershell, but I can't get it working. Here's an example of what I've tried to do:
$photo = ([ADSISEARCHER]“samaccountname=$($username)”).findone().properties.thumbnailphoto
$path = "C:\temp\Photo.jpg"
$photo | set-content $path -encoding byte
[Windows.System.UserProfile.UserInformation,Windows.System.UserProfile,ContentType=WindowsRuntime] > $null
[Windows.System.UserProfile.UserInformation]::SetAccountPictureAsync($photo)
I've tried a couple of other variations, but no matter what I do, I end up with an error like this:
Cannot convert argument "image", with value: "System.Object[]", for "setAccountPictureAsync" to type "Windows.Storage.IStorageFile" . . .
Is there something simple that I'm missing here to make this work?
I found this blog post by Keith Hill which seems like it might be helpful, but I am not sure if it directly translates to the issue I'm having.
Thanks!
Aurock
https://fleexlab.blogspot.com/2018/02/using-winrts-iasyncoperation-in.html has a pure-PowerShell solution.
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, #($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
This could then be used as:
$photoPath = "$home\Pictures\Photo.jpg"
$file = Await ([Windows.Storage.StorageFile]::GetFileFromPathAsync($photoPath)) ([Windows.Storage.StorageFile])
$result = Await ([Windows.System.UserProfile.UserInformation]::SetAccountPictureAsync($file)) ([Windows.System.UserProfile.SetAccountPictureResult])
SetAccountPicture expects an object implementing IStorageFile and not a byte array. I would save the picture to your Pictures folder then load that into a StorageFile as shown below. You should be able to pass that object into the SetAccountPicture() method e.g.
$photoPath = "$home\Pictures\Photo.jpg"
$asyncOp = [Windows.Storage.StorageFile]::GetFileFromPathAsync($photoPath)
$typeName = 'PoshWinRT.AsyncOperationWrapper[Windows.Storage.StorageFile]'
$wrapper = new-object $typeName -Arg $asyncOp
$file = $wrapper.AwaitResult()
$asyncOp = [Windows.System.UserProfile.UserInformation]::SetAccountPictureAsync($file)
$typeName = 'PoshWinRT.AsyncOperationWrapper[Windows.System.UserProfile.SetAccountPictureResult]'
$wrapper = new-object $typeName -Arg $asyncOp
$result = $wrapper.AwaitResult()
$wrapper.Dispose()

PowerShell says '%' and `New-Object` are not found. Am I missing an import? WinRM, or Exchange Role permission?

Suppose I already configured IIS to allow the remote runspace of "full", how do I resolve the exception that I'm getting where Powershell says "%" is not found.
Then when I comment out the offending for..each statement, it says New-Object can't be found.
Am I missing an import? Based on the comments, it's possible that I'm missing some configuration in WinRM, or an Exchange 2010 role permission.
public static void testExchangeMBScript()
{
PSCredential credential = new PSCredential(#"domain\me", createPassword("pw"));
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "exchangehost.company.com", 80, "/Powershell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
try
{
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
string ps1 = "Get-MailboxDatabase -Status";
string PSFull = #" $Databases = Get-MailboxDatabase -Status
foreach($Database in $Databases) {
$DBSize = $Database.DatabaseSize
$MBCount = #(Get-MailboxStatistics -Database $Database.Name).Count
$AllMBStats = Get-MailboxStatistics -Database $Database.Name
$MBItemAssocCount = $AllMBStats | %{$_.AssociatedItemCount.value} | Measure-Object -Average -Sum
$MBDeletedCount = $AllMBStats | %{$_.DeletedItemCount.value} | Measure-Object -Average -Sum
$MBItemCount = $AllMBStats | %{$_.ItemCount.value} | Measure-Object -Average -Sum
$MBDeletedItemSize= $AllMBStats | %{$_.TotalDeletedItemSize.value.ToMb() } | Measure-Object -Average -Sum
$MBItemSize = $AllMBStats | %{$_.TotalItemSize.value.ToMb()} | Measure-Object -Average -Sum
New-Object PSObject -Property #{
Server = $Database.Server.Name
DatabaseName = $Database.Name
UserCount = $MBCount
""DatabaseSize (GB)"" = $DBSize.ToGB()
""AverageMailboxSize (MB)"" = $MBItemSize.Average
""WhiteSpace (MB)"" = $Database.AvailableNewMailboxSpace.ToMb()
ItemCount = $MBItemCount.Sum
""LogicalSize (MB)"" = $MBItemSize.Sum
}
}
";
pipeline.Commands.AddScript(PSFull);
// This method cannot be called multiple times on a given pipeline. The state of the
// pipeline must be NotStarted when Invoke is called. When this method is called, it
// changes the state of the pipeline to Running.
// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms569128(v=vs.85).aspx
if (pipeline.PipelineStateInfo.State == PipelineState.NotStarted)
{
Collection<PSObject> results = pipeline.Invoke();
}
}
catch (RemoteException re)
{
// if: Assignment statements are not allowed in restricted language mode or a Data section.
// then: configure IIS application settings PSLanguageMode = FullLanguage
}
catch (Exception e)
{
}
}
The remote session is a constrained runspace. It doesn't allow you to use cmdlets you're wanting for your script. I believe what you'll need to do is use an unconstrained local runspace, and then from there run the Exchange management cmdlets in the remote runspace using invoke-command:
$AllMBStats = invoke-command {Get-MailboxStatistics -Database $databasename} -argumentlist $database.name -connectionuri "http://exchangehost.company.com/powershell"
Then work with the return from that in the local runspace.
Try creating a local runspace, and importing the Exchange 2010 commands from within that PS1
http://blogs.technet.com/b/exchange/archive/2009/11/02/3408653.aspx

Categories