How to logoff all windows 10 users except current logged in user? - c#

I am creating an windows software. In that I need to logoff all users except current user who is logged in.
I have tried using power shell command in my WPF application which works some time and and some time it doesn't.
find the command below:
quser | Select-String "Disc" | ForEach {logoff ($_.toString() -split ' +')[2]
When I click a button in my WPF Apllication all users except current should be loggedOff.

The code you posted will only log off disconnected users. To log off everyone except the current user you'll have to remove the current user from the quser output:
(quser | Select-Object -Skip 1) -notlike ">$env:USERNAME *"
and then log off the remaining sessions:
... | ForEach-Object { logoff ($_ -split ' +')[-5] }

Ansgars answer did not quite work for me, what worked was:
(quser) -notlike ">$env:USERNAME *" | Select-Object -Skip 1 | ForEach-Object { logoff ($_ -split ' +')[-5] }
This did the trick as I had to exclude the current user first from the quser listing.

Related

(Powershell & C#) ExchangeOnline Cmdlets partially working

The issue is a little awkward to explain but essentially I have a piece of C# code that connects & logs into ExchangeOnline via powershell (using System.Management.Automation.Powershell) from that I wish to run some more commands specific to the ExchangeOnline instance.
I've found that certain commands do not work such as "Get-Mailbox" or "Get-DistributionList". The error stream I have captured simply says the cmdlet does not exist. However if I try these commands in a powershell window it works.
The apparently problematic code I have running is pasted below (mix of both powershell and C#):
string FullTenantScript = #"
[string]$SizeOfAllMailboxes = ((get-exomailbox -ResultSize Unlimited | get-exomailboxstatistics).TotalItemSize.Value.ToMB() | measure-object -sum).sum
$TenantProperties = [pscustomobject]#{
'Primary Domain' = Get-AzureADDomain | Where-Object IsDefault -Match 'True' | Select -ExpandProperty Name
'Tenant ID' = Get-AzureADTenantDetail | select -ExpandProperty ObjectId
'Number of users' = #(Get-AzureADUser -all $true).count
'Number of Mailboxes' = #(get-exomailbox -ResultSize unlimited).Count
'Number of Devices' = #(Get-AzureADDevice -all $true).Count
'Number of Subscriptions' = #(Get-AzureADSubscribedSku).Count
'Number of Distribution Lists' = #(Get-DistributionGroup).Count
'Number of Dynamic DLs' = #(Get-DynamicDistributionGroup).Count
'Number of 365 Groups' = #(Get-UnifiedGroup).Count
'Size of all mailboxes' = $SizeOfAllMailboxes + ' MB'
}
$TenantProperties
";
PowerShell getTenantinfo = PowerShell.Create().AddScript(FullTenantScript);
Collection<PSObject> TenantInfoResult = getTenantinfo.Invoke();
I've read online that some of the older cmlets are being phased out (for example Get-Mailbox), so instead I use (get-exomailbox). I just don't get why I can run them in a powershell window but not in my program (as they should do essentially the same thing).
Please also note my program connects into Azure also (using Connect-AzureAD).
Any help or advice as to why this is happening would be greatly appreciated & any further info required I will provide.
Thanks,
Mouse

Calling entire feature file(or only when steps) into another feature file

I have been trying to call one base feature file which has steps which are repetitive across most of my other scenarios in suite.
Since base/common feature file has around 50 odd steps(based on manual TC) and I had to verify every page/element hence it's becoming very long feature file.
To avoid confusion I had divided entire base file into small sections by giving the scenario steps after every 4-5 steps to avoid the chain and added "#" as a prefix since I wanted whole file to execute as a single scenario. Is this right methodology or if somebody has better solution please share,
feature file 1
Scenario: Successful addition of an entry in list
Given User is on the login screen of app
When User enters valid Username and password
And user clicks on Log In
Then My Recent Submissions screen is displayed
And Add new submission form button should be displayed
#Scenario: Viewing Material information
When User clicks on Add new submission form (+) button
And a valid Material is searched using <visit> or <mrn>
And user clicks on Search
Then Search Result screen is displayed
#Scenario: Confirming the Material information and taking a photo
When User clicks Take Photo button
And user clicks on Use Photo
Then Image details screen is displayed
#Scenario: Selecting the facility name to reach New submission screen
When user clicks on Warehouse
And user clicks on Xyz Warehouse
Then New Submission screen is displayed
#Scenario: Confirm the Selected Facility to reach My Recent Submission Screen
When user clicks on Submit
Then Alert window pops up
When user selects Yes button on pop up screen
Then My Recent Submissions screen is displayed
And New Entry is added in list
Examples:
| Username | password | visit | mrn | Search | SearchByScanning |
| user1 | password_1 | 330045 | | Yes | No |
| user1 | password_1 | | 330045 | Yes | No |
| user1 | password_1 | | | |Yes |
| user1 | password_1 | | | |Yes |
All the above steps with user click on XXXXXX
and YYYYYY screen is displayed
XXXXXX and YYYYYY are the inline parameters which are used in step definition file inside methods to verify the pages with actual output and click on XXXXXX links/buttons
All the steps of feature file 1 are present across different/same step definition file in the format stated below,
[Then(#"(.*) screen is displayed")]
public void ThenApplicationShouldDisplayScreen(string expectedResult)
{
actualResult = SearchResult.GetTitle ();
Assert.AreEqual(expectedResult, actualResult);
}
feature file 2
Scenario: User verifies some other functionality
Given some other given statements
When user does some otherxyz operations
Then user gets some anotherabc output
Scenario:
Given User has created submission successfully #This line would call some of the steps from feature file 1
Given some other given statements
When user does some othermno operations
Then user gets some anotherpqr output
In another step definition file for (feature fle 2)
[Binding]
public class webConfigUpdation : Warehouse.MM.Test.MyRecentSubmissionsSteps #This would inherit all the steps present in this file as stated in link given below
{
[Given("User has created submission sucessfully")]
public void createSubmissionSuccessfully()
{
//All the other statements as per requirement that I can add over here using from feature file 1 which will in turn call step definitions mapped to each one of them
Then(#"My Recent Submissions screen is displayed");
}
}
I was trying the solution given by #samholder in another post with link
but was unable to implement it correctly. Am I making some silly mistake??
If anybody could share the solution would come very handy to me..
if you want to call your other steps you just need to call the steps. I'm not sure why this wouldn't work:
[Binding]
public class webConfigUpdation : Steps
{
[Given("User has created submission sucessfully")]
public void createSubmissionSuccessfully()
{
//just call all the steps you need here:
When("user clicks on Warehouse");
When("user clicks on Xyz Warehouse");
Then("New Submission screen is displayed");
When("user clicks on Submit");
Then("Alert window pops up");
When("user selects Yes button on pop up screen");
Then("My Recent Submissions screen is displayed");
}
}
Specflow will still use the regex to match the the steps.
Is there some specific problem with this that doesn't work?

Powershell to get list of users who last checked-in files into TFS2010

I'm trying to hack a quick script that runs stylecop to check C# style violations in a project, and then print out the name of the user who last checked it in into Team Foundation Server 2010.
I've gone as far as producing the list of files containing violations:
# using Stylecop from http://sourceforge.net/projects/stylecopcli/
StylecopCli\StyleCopCLI.exe -sln mysoluton.sln -set mysettings.StyleCop -out report.xml
[xml]$violations= Get-Content report.xml
$count = $violacoes.StyleCopViolations.Violation.Count
$filenames = $violacoes.StyleCopViolations.Violation | group Source -NoElement | sort Count -descending | % {$_.Name}
Any idea on how to do the last part? get the user who last checkedin into TFS? I've been looking at the TFS PowerTools and "tf.exe history" (https://msdn.microsoft.com/en-us/library/yxtbh4yh.aspx) but can't get the hang of it.
I believe this is what you're looking for:
$filenames | %{ tf history $_ /noprompt /recursive /stopafter:1 }

Specflow. Parametrized tests

I have such user story:
Given I am on the 'Login page'
When I enter following credentials
|Email |Pass |
|email1|pass1|
|email2|pass2|
Then I get next 'Error' messages
|ErrorMessage |
|ErrorMessage1|
|ErrorMessage2|
How can this be done?
The problem is that by my implementation, web driver is entering all the credentials from first table, and then try to assert error messages, but I need test to be run multiple times(kind of a loop).
you need to use a scenario outline:
Scenario Outline: some name...
Given I am on the 'Login page'
When I enter the email <email> and password <password>
Then I get next the error message <errorMessage>
Examples:
| email | password | errorMessage |
| email1| pass1 | ErrorMessage1|
| email2| pass2 | ErrorMessage2|
this will run the same test twice, once for each row in the Examples table. If you want the test to be run more times, just add more rows to the Examples table
As was pointed out in the comments, Examples is the defined term in gerkin, but Scenarios or Examples works fine in SpecFlow

I get an Access Denied error when calling LsaQueryInformationPolicy(), and I'm an admin

I get this error return whether I try LsaQueryInformationPolicy() on the local host or on some other machine in the domain. The flags I use for LsaOpenPolicy() are POLICY_LOOKUP_NAMES | POLICY_VIEW_LOCAL_INFORMATION but I also tried POLICY_LOOKUP_NAMES | POLICY_VIEW_LOCAL_INFORMATION | READ_CONTROL without success.
The user I'm logged in as is a local admin both on my machine and on all others I'm trying this.
For the second parameter to LsaQueryInformationPolicy() I use PolicyAuditEventsInformation.
I'm able to use LsaEnumerateAccountRights() against the same host, using the same policy handle, successfully.
Try to use POLICY_LOOKUP_NAMES | GENERIC_READ | POLICY_VIEW_LOCAL_INFORMATION instead of POLICY_LOOKUP_NAMES | POLICY_VIEW_LOCAL_INFORMATION. It works on my computer.
UPDATED: To be more exactly you need only POLICY_VIEW_AUDIT_INFORMATION during opening of LsaOpenPolicy() to read audit information.

Categories