我可以将变量的值称为powershell中的另一个变量吗?

时间:2017-09-07 18:42:21

标签: powershell variables

我有3个环境可供使用,需要根据我所处的环境选择一个值。我有一个动态变量$development $test $production ,可以是开发,测试或生产。我还想要其他变量:

public class Geolocalizcao extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {


private GoogleMap mMap;
private GoogleApiClient client;
private LocationRequest locationRequest;
private Location lastLocation;
private Marker currentLocationMarker;
public static final int REQUEST_LOCATION_CODE = 99;
public static int REQUEST_ERROR_CODE;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_geolocalizcao);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.

    Intent i = getIntent();
    CarDataset cardata = (CarDataset) i.getExtras().getParcelable("select");

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);



}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        buildGoogleApiClient();

        return;
    }

}

protected synchronized void buildGoogleApiClient()
{
    client = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
    client.connect();
}

@Override
public void onLocationChanged(Location location) {

    LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Here!");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker());

    currentLocationMarker = mMap.addMarker(markerOptions);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomBy(3));

    if(client != null)
    {
        LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    locationRequest = new LocationRequest();

    locationRequest.setInterval(1000);
    locationRequest.setFastestInterval(1000);
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);


}

public boolean checkLocationPermission()
{
    if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_LOCATION_CODE);
        }
        else
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_LOCATION_CODE);
        }
        return false;
    }
    else
        return false;
}

@Override
// When connection is lost...
public void onConnectionSuspended(int i)
{
    Toast.makeText(this, "Lost connection. Trying to reconnect...", Toast.LENGTH_SHORT);
    client.connect();
}

@Override
// Called when the API client doesnt sucessufly connect
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{

    if (!connectionResult.hasResolution())
    {
        GoogleApiAvailability.getInstance().getErrorDialog(this, connectionResult.getErrorCode(), 0).show();
        return;
    }
    try
    {
        connectionResult.startResolutionForResult(this, REQUEST_ERROR_CODE );
    } catch (IntentSender.SendIntentException e)
    {
        Log.e("LOG", "Exception:", e);
    }
}

根据我工作的环境使用值。

有没有办法调用$($ environment)并将环境变量的结果用作另一个变量?澄清:

$ abc =" prod" $ prod ="生产价值"

我想打电话给$($ abc)之类的东西,然后返回"生产价值"

类似的问题被问到NSNotificationName但是对于Ruby;有没有办法在Powershell中做到这一点?

1 个答案:

答案 0 :(得分:3)

您可以使用Get-Variable

执行此操作
$environment = 'test'

$test = 'test'
$prod = 'prod'

(Get-Variable -Name $environment).value

将返回$ test变量的值。